-1

in my xaml code I got two places which look very similiar and I'm trying to refactor them to not have duplicated code, but turns out it's not that easy...

                   <DataTemplate >
                       <Image Width="16" Height="16" RenderOptions.BitmapScalingMode="HighQuality">
                           <Image.Style>
                               <Style TargetType="{x:Type Image}">
                                   <Style.Triggers>                                           
                                       <DataTrigger Binding="{Binding Path=TypeDescription}" Value="Matlab/Simulink">
                                           <Setter Property="Source" Value="xxx.png"/>
                                       </DataTrigger>
                                       <DataTrigger Binding="{Binding Path=TypeDescription}" Value="Python">
                                           <Setter Property="Source" Value="yyy.png"/>
                                       </DataTrigger>
                                       <DataTrigger Binding="{Binding Path=TypeDescription}" Value="High Speed Function">
                                           <Setter Property="Source" Value="zzzpng"/>
                                       </DataTrigger>
                                   </Style.Triggers>
                               </Style>
                           </Image.Style>
                       </Image>
                   </DataTemplate>

and

<DataTemplate DataType="dxg:GridCellData">
          <Image Width="16" Height="16" RenderOptions.BitmapScalingMode="HighQuality">
            <Image.Style>
              <Style TargetType="{x:Type Image}">
                <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=Data.TypeDescription}" Value="Matlab/Simulink">
                    <Setter Property="Source" Value="xxx.png"/>
                  </DataTrigger>
                  <DataTrigger Binding="{Binding Path=Data.TypeDescription}" Value="Python">
                    <Setter Property="Source" Value="yyy.png"/>
                  </DataTrigger>
                  <DataTrigger Binding="{Binding Path=Data.TypeDescription}" Value="High Speed Function">
                    <Setter Property="Source" Value="zzz.png"/>
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </Image.Style>
          </Image>
        </DataTemplate>

Notice the only difference is the path of the binding. I'm trying to move this datatemplate to another file like resources and just reuse it, but the path of the binding is really making things complicated. Can anybody help me?

Thanks

user3292642
  • 711
  • 2
  • 8
  • 32
  • 1
    I would probably try to set the datacontext of wherever the datatemplate is used to the "Data" part of the binding in the second example. Then you would be able to use the first example's binding for general usage. – Lloyd Aug 19 '19 at 11:54

1 Answers1

0

I am afraid you can't just replace the binding path and reuse the rest of the template in pure XAML because templates must be defined as-a-whole.

What you can do is to define the template programmatically using the XamlReader.Parse method:

Combining DataTemplates at runtime

You may then simply replace a part of the string that you pass to the Parse method before you add the templates to your ResourceDictionary.

mm8
  • 163,881
  • 10
  • 57
  • 88