0

I have this code that always display the default datatemplate even when the binding type seem to be LineRenderableSeriesViewModel.

 <ListBox ItemsSource="{Binding ElementName=ACList, Path=SelectedItem.seriesViewModels, Mode=OneWay}"  IsSynchronizedWithCurrentItem="True">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <StackPanel.Resources>
                                    <DataTemplate x:Key="Default"  >
                                        <Border BorderBrush="Red" BorderThickness="2" Background="Pink">
                                            <TextBlock Text="{Binding}" Foreground="Red" FontWeight="Bold" Padding="5"/>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate  x:Key="BindedTemplate1"  >
                                        <Border BorderBrush="Green" BorderThickness="2" CornerRadius="5">
                                            <TextBlock Text="I'm asssociated to LineRenderableSeriesViewModel " Foreground="Green" FontWeight="Bold" Padding="5"/>
                                        </Border>
                                    </DataTemplate>
                                </StackPanel.Resources>
                                <!-- atempt to write type of object -->
                                <Label Content="{Binding}" ContentStringFormat="MyBindingType : {0}"></Label>
                                <!-- atempt to display the associated template -->
                                <ContentControl Content="{Binding}">
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Setter Property="ContentTemplate" Value="{StaticResource Default}"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="LineRenderableSeriesViewModel">
                                                    <Setter Property="ContentTemplate" Value="{StaticResource BindedTemplate1}"/>
                                                </DataTrigger>                                                
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>

                            </StackPanel>
                        </DataTemplate>
                   
                        
                    </ListBox.ItemTemplate>
                </ListBox>

the display show rigth value of the viewmodel but it take default datatemplate enter image description here

im not sure what hapen ?

Zwan
  • 632
  • 2
  • 6
  • 23
  • `` would work if the *value* - not the type - of the current item would somehow be LineRenderableSeriesViewModel. Instead of this strange construct, there should be a set of DataTemplate resources which have their DataType set appropriately. Do not explicity set the ItemTemplate property. Alternatively, use a DataTemplateSelector. – Clemens Apr 09 '21 at 15:45

1 Answers1

1

You can do it a lot easier.
Just define the datatemplates with the attribute DataType and the binding engine does the rest for you.

<ListBox ItemsSource="{Binding Path=ACList}" >
  <ListBox.Resources>
    <DataTemplate DataType="{x:Type local:Other}" >
      <Border BorderBrush="Red" BorderThickness="2" Background="Pink">
        <TextBlock Text="{Binding}" Foreground="Red" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:LineRenderableSeriesViewModel}">
      <Border BorderBrush="Green" BorderThickness="2" CornerRadius="5">
        <TextBlock Text="I'm asssociated to LineRenderableSeriesViewModel " Foreground="Green" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
  </ListBox.Resources>
</ListBox>

Just remember to exchange "local" with the namespace definition, where your classes are located

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • i did change but it still not active i think the type is DataBinding or something like this but not the one i real wait i dont know how to check in label if the the type is the one it should be – Zwan Apr 09 '21 at 16:04
  • 1
    Remove the key from the DataTemplate and it should work – Flat Eric Apr 09 '21 at 16:07
  • yes i used different technique but it work now prolly cuz of a key was set thanks – Zwan Apr 09 '21 at 16:14