0

I want selected item in ComboBox look differently from it's instance in drop down list.

    <ComboBox ItemsSource="{Binding ViewList}" SelectedItem="{Binding SelectedView}">
        <ComboBox.Resources>
            <DataTemplate DataType="{x:Type vm:View}">
                <StackPanel Orientation="Horizontal">
                    <c:Icon x:Name="Icon" IconShape="{DynamicResource z.Users}" Margin="5,0" Background="{Binding Foreground, RelativeSource={RelativeSource Self}}"/>
                    <StackPanel>
                        <StackPanel>
                            <TextBlock x:Name="CurrentView" Text="Current View"
                                       Foreground="{DynamicResource Pallete.Primary.Brighter}"
                                       Visibility="{Binding IsSelected, Converter={StaticResource bool2VisibilityConverter}}"/>
                            <TextBlock x:Name="Title" Text="{Binding Title}"/>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Type}" Value="SeparatorView">
                        <Setter TargetName="Icon" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="Title" Property="FontWeight" Value="Bold"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Type}" Value="YearView">
                        <Setter TargetName="Icon" Property="IconShape" Value="{DynamicResource z.Bookmark}"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ComboBox.Resources>
    </ComboBox>

Is there something I could use in CurrentView's Visibility Property or in triggers?

zORg Alex
  • 372
  • 2
  • 15
  • It's a duplicate for: https://stackoverflow.com/questions/3995853/how-to-display-a-different-value-for-dropdown-list-values-selected-item-in-a-wpf, I believe. The solution with template selector is very clever and simple: https://stackoverflow.com/a/3996627/275330 – Maciek Świszczowski Jul 07 '17 at 14:29

1 Answers1

1

Here is an example of your you could modify the Foreground of a TextBlock when it it is selected in the ComboBox using a DataTrigger:

<ComboBox ItemsSource="{Binding ListOfStrings}">
    <ComboBox.Resources>
        <DataTemplate DataType="{x:Type sys:String}">
            <TextBlock x:Name="txt" Text="{Binding}" Foreground="Red" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="{x:Null}">
                    <Setter TargetName="txt" Property="Foreground" Value="Green"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ComboBox.Resources>
</ComboBox>

I believe that the following is what you are looking for:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="{x:Null}">
mm8
  • 163,881
  • 10
  • 57
  • 88