tim*_*tim 12 wpf combobox visibility
仅在选择组合中的某个项目时尝试显示标签.代码应该解释它.
<ComboBox Name="comboMyCombo">
<ComboBoxItem>Don't show the label</ComboBoxItem>
<ComboBoxItem>Show the label</ComboBoxItem>
</ComboBox>
<Label Visibility="Collapsed">This is my label
<Label.Style>
<Style>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=comboMyCombo, Path=SelectedValue}" Value="Show the label">
<Setter Property="Label.Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Run Code Online (Sandbox Code Playgroud)
Sco*_*t J 27
这里有两个问题.首先,应在样式中指定默认可见性.但即便如此,它也无法工作,因为触发器上的绑定是将SelectedValue,ComboBoxItem对象与字符串对象进行比较,而且永远不会等效.为了简化示例,我在ComboBoxItem的Tag属性中放置了适当的值.虽然比较的实际实施可能会根据应用程序的特定需求而有所不同.
<ComboBox Name="comboMyCombo">
<ComboBoxItem Tag="Hide">Don't show the label</ComboBoxItem>
<ComboBoxItem Tag="Show">Show the label</ComboBoxItem>
</ComboBox>
<Label>This is my label
<Label.Style>
<Style>
<Setter Property="Label.Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=comboMyCombo, Path=SelectedItem.Tag}" Value="Show">
<Setter Property="Label.Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Run Code Online (Sandbox Code Playgroud)
edv*_*dig 10
一个"更清洁"的解决方案
<ComboBox>
<ComboBoxItem x:Name="iOne" Content="One"/>
<ComboBoxItem x:Name="iTwo" Content="Two"/>
<ComboBoxItem x:Name="iThree" Content="Three"/>
</ComboBox>
<Label Content="One is shown">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=iOne, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Run Code Online (Sandbox Code Playgroud)