如何确定ComboBox项目是在下拉列表中还是在其中

时间:2017-07-07 09:31:47

标签: wpf combobox styles datatemplate

我希望ComboBox中的所选项目与下拉列表中的实例不同。

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

我可以在CurrentView的可见性属性或触发器中使用吗?

1 个答案:

答案 0 :(得分:1)

以下是您使用ForegroundTextBlock中选择ComboBox时修改DataTrigger的{​​{1}}的示例:

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

我相信您正在寻找以下内容:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="{x:Null}">