隐藏WPF应用程序中的所有按钮

本文关键字:按钮 WPF 应用程序 隐藏 | 更新日期: 2023-09-27 18:18:01

我有一个带有一些视图(xaml)的WPF c#应用程序,其中包括一个选项视图。在这个视图中有一个复选框来隐藏所有按钮。

问题是,如何做到这一点?

我有一个MainStyle.xaml,它是一个ResourceDictionary,包含所有样式,转换器等。我的方法是在这个文件中设置一个样式,如:

<Style TargetType="Button">
    <Setter Property="Visibility" Value="Collapsed" />
</Style>

这是可行的,但是用户应该决定按钮是否可见。所以我必须将样式绑定到复选框。

第一步将样式绑定到ResourceDictionary (MainStyle.xaml)后面的代码。但这行不通。我在构造函数中将属性设置为false,但是按钮是可见的。

MainStyle.xaml

<Style TargetType="Button">
    <Setter Property="Visibility" 
            Value="{Binding ButtonsEnabled, RelativeSource={RelativeSource AncestorType=ResourceDictionary}, Converter={StaticResource BooleanVisibilityConverter}}" />
</Style>

背后的代码(MainStyle.xaml.cs)

public partial class BaseStyle : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public BaseStyle()
    {
        InitializeComponent();
        ButtonsEnabled = false; // for testing
    }
    private Boolean buttonsEnabled;

    public bool ButtonsEnabled
    {
        get { return buttonsEnabled; }
        set
        {
            buttonsEnabled = value;
            NotifyPropertyChanged("ButtonsEnabled");
        }
    }
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

如果绑定有效,第二步就是将ResourceDictionary中的样式绑定到options视图。

隐藏WPF应用程序中的所有按钮

如果不能直接从绑定中找到复选框,则可以使用带有继承的附加属性。例如,下面的XAML:

<UserControl x:Class="WpfSpikes.InheritedAttachPropertyView" x:Name="View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfSpikes"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <Style x:Key="VisibleButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=(local:ButtonVisibility.IsVisible), Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <CheckBox Content="Show Buttons?" IsChecked="{Binding ElementName=View, Path=(local:ButtonVisibility.IsVisible), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Button Grid.Row="1" Content="Button1" Style="{StaticResource VisibleButtonStyle}"/>
        <Button Grid.Row="2" Content="Button2" Style="{StaticResource VisibleButtonStyle}"/>
        <Button Grid.Row="3" Content="Button3" Style="{StaticResource VisibleButtonStyle}"/>
        <Button Grid.Row="4" Content="Button4" Style="{StaticResource VisibleButtonStyle}"/>
    </Grid>
</UserControl>

使用附加属性:

public static class ButtonVisibility
{
    public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(ButtonVisibility), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
    public static bool GetIsVisible(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsVisibleProperty);
    }
    public static void SetIsVisible(DependencyObject obj, bool value)
    {
        obj.SetValue(IsVisibleProperty, value);
    }
}

控制按钮的可见性。

注意,附加的属性指定FrameworkPropertyMetadataOptions.Inherits,并且组合框绑定到名为View的元素上的此属性(即顶级用户控件)。然后,按钮样式在每个单独按钮的级别上绑定到这个属性(使用RelativeSource={RelativeSource Self}),因为按钮在UserControl中,它会获取IsVisible的继承值。

适合我。