gpt4 book ai didi

基于组合选择的 UI 元素的 WPF 可见性

转载 作者:行者123 更新时间:2023-12-04 00:03:41 25 4
gpt4 key购买 nike

Trying to show a label only when a certain item in a combo is selected.代码应该几乎可以解释它。

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

最佳答案

这里有两个问题。首先,应在样式中指定默认可见性。但即便如此,它也不会起作用,因为触发器上的绑定(bind)正在比较 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>

关于基于组合选择的 UI 元素的 WPF 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com