Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controls with TemplateSelector property

Tags:

wpf

xaml

controls

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?

like image 691
Seekeer Avatar asked Dec 28 '22 10:12

Seekeer


1 Answers

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 =)

like image 162
Louis Kottmann Avatar answered Jan 12 '23 01:01

Louis Kottmann