如何使用相同的DataTemplate并绑定到外部动态属性 [英] How to use the same DataTemplate and bind to outside dynamic properties

查看:40
本文介绍了如何使用相同的DataTemplate并绑定到外部动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功设置了数据网格以支持过滤.我所做的就是创建一个 DataTemplate ,其中包含一个切换按钮和一个弹出窗口,其中弹出窗口带有 TextBox 作为过滤器搜索框.我在< DataGrid.Resources></DataGrid.Resources>

I successfully set up my datagrid to support filtering. All I did was create a DataTemplate that contained a toggle button and a popup window with a TextBox serving as the filter search box. I created two for each column (total of 2) this way in the <DataGrid.Resources></DataGrid.Resources>

这是我的xaml:

    <DataTemplate x:Key="FooHeaderTemplate">
                        <Grid Margin="0, 0, -5, 0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <ContentControl Content="{Binding}" VerticalAlignment="Center"/>
                            <ToggleButton Name="FooFilterButton"  
                                          Grid.Column="1"  
                                          Width="25"
                                          Height="25"
                                          Margin="0, 1, 5, 1"
                                          Padding="1, 0" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}">
                                <ToggleButton.Content>
                                    <Image Stretch="Fill">
                                        <Image.Style>
                                            <Style TargetType="Image">
                                                <Style.Triggers>
                                                    <DataTrigger 
                                                        Binding="{Binding DataContext.IsFooFilterApplied, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True">
                                                        <Setter Property="Source" Value="/Foo.Bar.FooApplication;component/Resources/filter-applied.png"/>
                                                    </DataTrigger>

                                                    <DataTrigger 
                                                        Binding="{Binding DataContext.IsFooFilterApplied, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="False">
                                                        <Setter Property="Source" Value="/Foo.Bar.FooApplication;component/Resources/filter-doff.png"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                </ToggleButton.Content>
                            </ToggleButton>
                            <Popup IsOpen="{Binding ElementName=FooFilterButton, Path=IsChecked}" PlacementTarget="{Binding ElementName=FooFilterButton}" Placement="Left" StaysOpen="False" HorizontalAlignment="Right">
                            <Border Background="White" Padding="3">
                                    <TextBox Width="300" Text="{Binding DataContext.FooFilterSearchBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                </Border>
       </Popup>
    </Grid>
</DataTemplate>

如您所见,我具有绑定到与特定列相关的 DataTemplate 的唯一属性.我复制了这个 DataTemplate ,并更改了它的名称和属性绑定以适合下一列.

As you can see, I have unique properties bound to this DataTemplate that relates to a particular column. I duplicated this DataTemplate and changed its Name and the property binding to suit the next column.

按目前的样子,它应该像应该的那样工作.我单击过滤器按钮,出现弹出窗口,输入一个字符串值,并且键入时表数据会更新.此外,过滤器按钮的图形会更改以指示何时将过滤器应用于该特定列.

As it stands, it works like it should. I click on the filter buttons and the pop up appears, I enter a string value and the table data updates as I type. What's more, The graphic for the filter button changes to indicate when a filter is applied for that particular column.

问题是我必须重复相同的 DataTemplate .如果只有两列要过滤,但是在具有7列的 DataGrid 中又如何,我需要为每列添加一个过滤器,那我猜很好吗?

The problem is that I had to duplicate the same DataTemplate. I guess its fine if I only have two columns to filter but what about in a DataGrid with 7 columns and I need to add a filter for each?

我的问题是这样:我有什么办法可以只使用一个 DataTemplate ,然后在每列中对其进行修改?

My question is this: is there any way I use just one DataTemplate and then modify it per column?

非常感谢!

推荐答案

我的问题是这样的:我有什么办法可以只使用一个DataTemplate然后在每列中对其进行修改?

My question is this: is there any way I use just one DataTemplate and then modify it per column?

简短的回答:不,至少不是在XAML中.您必须整体定义模板.恐怕您只能继承"绑定以外的所有部分.

Short answer: No, at least not in XAML. You must define a template as a whole. I am afraid you can't "inherit" all parts but the binding.

您可以考虑创建模板以编程方式:

有没有办法仅使用C#构建数据模板

这篇关于如何使用相同的DataTemplate并绑定到外部动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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