How to use the same DataTemplate and bind to outside dynamic properties

非 Y 不嫁゛ 提交于 2021-01-29 08:02:05

问题


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>

Here is my 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>

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.

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?

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

Many thanks in advance!


回答1:


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

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.

You may consider creating the templates programmatically:

Is there a way to build a DataTemplate with only C#



来源:https://stackoverflow.com/questions/62429523/how-to-use-the-same-datatemplate-and-bind-to-outside-dynamic-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!