Home > Mobile >  How to create a template with parameters in WPF?
How to create a template with parameters in WPF?

Time:11-24

I have 3 List views. They are very similar. The only difference is that their ItemsSource binds to different variables. Is there a way to create a template list view with unknown ItemsSource, and I can pass a parameter to fill that ItemsSource?

My code is something like this:

<ListView Name="View1" ItemsSource={"Binding Student1"}>
    <TextBlock Text={"Binding Name"}/>
</ListView>

<ListView Name="View2" ItemsSource={"Binding Student2"}>
    <TextBlock Text={"Binding Name"}/>
</ListView>

<ListView Name="View3" ItemsSource={"Binding Student3"}>
    <TextBlock Text={"Binding Name"}/>
</ListView>

I am thinking if there is a way to create something like:

<ListView Name="StudentTemplate" ItemsSource=Parameter1>
    <TextBlock Text={"Binding Name"}/>
</ListView>

Then I can use it like:

<Temaplate="StudentTemplate", Parameter="{Binding Student1}"/>
<Temaplate="StudentTemplate", Parameter="{Binding Student2}"/>
<Temaplate="StudentTemplate", Parameter="{Binding Student3}"/>

Something like that. I know my syntax makes no sense. I am new to WPF. I am thinking of create a function in C where it takes a parameter. Hopefully it makes sense.

CodePudding user response:

use ContentPresenter :

<Grid>
      <ContentPresenter Content="{Binding Student1}" ContentTemplate="{StaticResource YourTemplate}"/>
</Grid>
<Grid>
      <ContentPresenter Content="{Binding Student2}" ContentTemplate="{StaticResource YourTemplate}"/>
</Grid>

Then your template will look like that :

<DataTemplate x:Key="YourTemplate">
    <Grid Background="{StaticResource WindowBackgroundColor}">
         <ListView ItemsSource="{Binding}">
            //...
         </ListView>
    </Grid>
</DataTemplate>

Like that, you have a template that is binded to Student1, Student2...

CodePudding user response:

You are on the right track with thinking about templating

What you are looking for is something called a ControlTemplate.

Your ControlTemplate would then target the ListView control and use the key word TemplateBinding to pass through the ItemsSource binding from your ListView

You would look to add this as a window resource as shown below.

<Window.Resources>
    <ControlTemplate x:Key="ListViewTemplate" TargetType="ListView">
        <ListView ItemsSource="{TemplateBinding ItemsSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ControlTemplate>
</Window.Resources>

This would enable you to use this template on your ListView controls as shown below

<ListView Template="{StaticResource ListViewTemplate}" ItemsSource="{Binding PersonList}"/>
<ListView Template="{StaticResource ListViewTemplate}" ItemsSource="{Binding PersonList1}"/>
<ListView Template="{StaticResource ListViewTemplate}" ItemsSource="{Binding PersonList2}"/>

Hope this gives you what you were looking for

  • Related