XAML: custom binding in a DataTemplate for use in a GridViewColumn CellTemplate

I have the following DataTemplate in resources that I would like to reuse in a GridView.

<Window.Resources>
  <DataTemplate x:Key="NumericalDataTemplate" DataType="GridViewColumn.CellTemplate">
     <StackPanel Orientation="Horizontal" Height="32">
       <TextBlock Text="{Binding MyLength}" VerticalAlignment="Center"
                     HorizontalAlignment="Center">
         <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}" >
               <Setter Property="Visibility" Value="Visible" />
               <Style.Triggers>
                 <DataTrigger Binding="{Binding PropertyEditable}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                 </DataTrigger>
               </Style.Triggers>
           </Style>
         </TextBlock.Style>
       </TextBlock>
    </StackPanel>
  </DataTemplate>
</Window.Resources>

What is implemented as follows.

<GridViewColumn Header="MyLength" Width="80" 
                CellTemplate="{StaticResource NumericalDataTemplate}" />

I would like to change the TextBlock binding (currently Text={Binding MyLength}) so that it can use a custom binding for each GridViewColumn cell template (e.g. MyHeight, MyWeight, etc.).

The way I intended to do this is to change the TextBlock binding to just use {Binding}and have a GridViewColumn, set the binding. However, I'm not sure where and how to do this, since setting DisplayMemberValue to {Binding MyLength}(for example) just cancels the pattern.

I would like to do this completely in XAML.

+1
1

, CellTemplate , DisplayMemberBinding. , @HB . - #/VB, XAML.

# , @H.B. , XAML, :

<Window ......
    xmlns:local="clr-namespace:WpfProject">

DataTemplate TextBlock :

<DataTemplate x:Key="TemplateBuilder_BaseTemplate" DataType="GridViewColumn.CellTemplate">
    <StackPanel Orientation="Horizontal" Height="32">
        <TextBlock Text="{local:TemplateBuilderTag}" VerticalAlignment="Center"
                   HorizontalAlignment="Center">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}" >
                    <Setter Property="Visibility" Value="Visible" />
                    <Setter Property="Foreground" Value="Red"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding PropertyEditable}" Value="True">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </StackPanel>
</DataTemplate>

DataTemplate :

<GridView.Columns>
    <GridViewColumn Header="MyLength" Width="80"
            CellTemplate="{local:TemplateBuilder MyLength}" />
    <GridViewColumn Header="MyHeight" Width="80" 
            CellTemplate="{local:TemplateBuilder MyHeight}" />
</GridView.Columns>
+2

All Articles