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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2561820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:38:46  来源:igfitidea点击:

WPF Visibility of a UI element based on combo selection

wpfcomboboxvisibility

提问by tim

Trying to show a label only when a certain item in a combo is selected. Code should pretty much explain it.

仅在选择组合中的某个项目时才尝试显示标签。代码应该几乎可以解释它。

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

回答by Scott J

There are two issues here. First the default visibility should be specified in the style. But even with that it won't work because the binding on the trigger is comparing a SelectedValue, a ComboBoxItem object with a string object and that will never be equivalent. To keep the example simple, I've placed appropriate values in the ComboBoxItem's Tag properties. Although the actual implementation of the comparison will likely vary based on the specific needs of the app.

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

回答by edvaldig

A "cleaner" solution would be

“更清洁”的解决方案是

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