Now I have ListView and in one column has:
<GridViewColumn CellTemplateSelector="{StaticResource messagerEditorTemplateSelector}"/>
And everything is fine: cell is filled with content based on item. But now I want to place in this cell 2 controls: for one template must be selected based on binding and other is control with name, say TimeRangeView
. But I cannot understand how it can be implemented? So I must have code like:
<GridViewColumn>
<DataTemplate>
<StackPanel>
<SomeControlWhichSupportTemplateSelector ... />
<views:TimeRangeView ... />
</StackPanel>
</DataTemplate>
</GridViewColumn>`
Which control should I use for template? I've found only listbox but it must be bound to collection. Of course, I could bind like:
<ListBox ItemsSource="{Binding Converter=ItemToCollectionConverter}" />
but it doesn't look elegant. May be there is another way to do it?
You can use a ContentControl and set its ContentTemplateSelector property:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ContentControl ContentTemplateSelector="{StaticResource messagerEditorTemplateSelector}" />
<views:TimeRangeView ... />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Note that for Binding to work within your ContentControl, you will have to set the Content
property to whichever object is used in the Bindings of the DataTemplate returned by your selector.
So that's 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>
Which is what i do and it works like a charm =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With