如何在运行时隐藏标签



我有数据模板,其中有一些标签,我想做的是隐藏一些标签在运行时根据配置设置。

我已经将标签的可见性绑定到一个属性,但是即使属性为False,标签也不会隐藏。

下面是我的xaml
<Label x:Name="lblWashingMachineName" Content="{x:Static Resources:Translations.MainWindow_WashingMachineName}"
                            Grid.Row="6" Grid.Column="2" Style="{StaticResource styleLabelBig}" Visibility="{Binding Path=ShowLabels}"></Label>
财产

    public bool ShowLabels
    {
        get
        {
            return _showLabels;
        }
        private set
        {
            _showLabels = value;
            OnPropertyChanged("ShowLabels");
        }
    }

在构造函数

中设置属性
    public DisplayScopeRecord()
    {
        ShowLabels = !(AppContext.Instance.DicomizerEnabled);
    }

你的变量是一个布尔值,但可见性是一个enum(可见,隐藏,崩溃)。你需要使用。net内置的BooleanToVisibilityConverter将布尔值转换为可见性。

<BooleanToVisibilityConverter x:Key="BoolToVis" />
<Label x:Name="lblWashingMachineName" Content="{x:Static Resources:Translations.MainWindow_WashingMachineName}"
                        Grid.Row="6" Grid.Column="2" Style="{StaticResource styleLabelBig}" Visibility="{Binding Path=ShowLabels, Converter={StaticResource BoolToVis}}"/>

得到它我的属性需要的类型为可见性为它的工作

财产

    public Visibility ShowLabels
    {
        get
        {
            return _showLabels;
        }
        private set
        {
            _showLabels = value;
            OnPropertyChanged("ShowLabels");
        }
    }
构造函数

    public DisplayScopeRecord()
    {
        if (AppContext.Instance.DicomizerEnabled)
        {
            ShowLabels = Visibility.Hidden;
        }
    }

你应该覆盖你的标签的样式,并在你的ShowLabels属性的值上设置一个数据触发器。

<Label x:Name="lblWashingMachineName" Content="{x:Static Resources:Translations.MainWindow_WashingMachineName}" Grid.Row="6" Grid.Column="2">
    <Label.Style>
        <Style BasedOn="{StaticResource styleLabelBig}" TargetType="{x:Type Label}">
            <Setter Property="Visibility" Value="Visible" />
                <Style.Triggers>
                     <DataTrigger Binding="{Binding Path=ShowLabels}" Value="False">
                          <Setter Property="Visibility" Value="Collapsed" />
                     </DataTrigger>
                </Style.Triggers>
         </Style>
    </Label.Style>
</Label>

最新更新