Controls with the TemplateSelector property

Now I have a ListView and in one column there is:

<GridViewColumn CellTemplateSelector="{StaticResource messagerEditorTemplateSelector}"/>

And that's all right: the cell is filled with content based on the element. But now I want to place 2 controls in this cell: for one template, it must be selected based on the binding, and the other with a name, for example TimeRangeView . But I do not understand how this can be implemented? So I should have code like:

 <GridViewColumn> <DataTemplate> <StackPanel> <SomeControlWhichSupportTemplateSelector ... /> <views:TimeRangeView ... /> </StackPanel> </DataTemplate> </GridViewColumn>` 

What control should be used for a template? I found only the list, but it should be attached to the collection. Of course, I could communicate like:

<ListBox ItemsSource="{Binding Converter=ItemToCollectionConverter}" />

but he does not look elegant. Maybe there is another way to do this?

+4
source share
1 answer

You can use ContentControl and set its ContentTemplateSelector property:

  <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <ContentControl ContentTemplateSelector="{StaticResource messagerEditorTemplateSelector}" /> <views:TimeRangeView ... /> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 

Note that to bind to work in your ContentControl, you will need to set the Content property depending on which object is used in the DataTemplate bindings returned by your selector.

So, for option 1.
You can also just use DataTriggers:

  <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <ContentControl Content="{Binding MyBoundObject}"> <ContentControl.Style> <Style> <Style.Triggers> <DataTrigger Binding="{Binding Path=MyBoundObject.ABooleanProperty}" Value="True"> <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource myFirstTemplate}" /> </DataTrigger> <DataTrigger Binding="{Binding Path=MyBoundObject.ABooleanProperty}" Value="False"> <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource mySecondTemplate}" /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> <views:TimeRangeView ... /> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 

This is what I do and it works like a charm =)

+9
source

Source: https://habr.com/ru/post/1384298/


All Articles