更改模板内的元素 [英] Change element inside Template

查看:72
本文介绍了更改模板内的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用ListView中的ItemsControl,将ItemsControle放入模板中并不是很难.
问题是,我要在使用ItemsControlTemplate时更改DataTemplate.

I want to reuse the the ItemsControl from my ListView and its not really hard to put the ItemsControle into an Template.
The problem is, I want to change the DataTemplate when I use my ItemsControlTemplate.

<ListView DataContext="{Binding Input}">
                        <ItemsControl ItemsSource="{Binding Columns}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>
                                        <StackPanel Grid.Row="0">
                                            <Label Content="{Binding ColumnHeader}"></Label>
                                        </StackPanel>
                                        <ListView Grid.Row="1">
                                            <ItemsControl ItemsSource="{Binding ColumnData}">
                                                <ItemsControl.ItemTemplate>

                                                    <!--This should be changed when I reuse my ItemsControl-->
                                                    <DataTemplate>
                                                        <Button Content="{Binding}"/>
                                                    </DataTemplate>


                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </ListView>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ListView>

推荐答案

您可以使用Resource来动态设置该ItemTemplate.只需提供默认的Resource DataTemplate,当您需要更改它时,覆盖该资源(使用相同的键并将其放置在距控件较近的位置).

You can use a Resource to set that ItemTemplate dynamically. Just provide a default Resource of DataTemplate, when you need to change it, override that resource (using the same key and place in a closer place to the control).

 <ListView Grid.Row="1">
    <ItemsControl ItemsSource="{Binding ColumnData}"
                  ItemTemplate="{StaticResource templateKey}"/>
 </ListView>

然后像这样定义x:KeytemplateKey的资源:

Then define a resource with x:Key of templateKey like this:

 <Window.Resources>
   <DataTemplate x:Key="templateKey">
     <Button Content="{Binding}"/>
   </DataTemplate>
 <Window.Resources>

这里是一个覆盖模板的示例,其中我们在ListView的资源中放置了另一个DataTemplate:

Here is an example of overriding the template, in which we place another DataTemplate inside the ListView's Resources:

<ListView DataContext="{Binding Input}">
   <ListView.Resources>
     <DataTemplate x:Key="templateKey">
       <CheckBox Content="{Binding}"/>
     </DataTemplate>
   </ListView.Resources>
   <!-- ... -->
</ListView>

如果要在运行时更新模板,则需要使用DynamicResource而不是StaticResource.

If you want to update the template at runtime, you need to use DynamicResource instead of StaticResource.

这篇关于更改模板内的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆