我有一个WPF C#应用程序,其中包含一些视图(xaml),尤其是选项视图。在这个视图中有一个复选框来隐藏所有按钮。隐藏WPF应用程序中的所有按钮
问题,该怎么做?
我有一个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到选项视图。
首先检查您的VS输出窗口,以搜索失败的绑定。然后启动Snoop并检查Visibility属性中是否有失败的按钮(以红色突出显示)。如果你能发现真正的失败,那么你可以开始正确的解决方案。 – LordWilmore