【发布时间】:2014-01-01 10:29:23
【问题描述】:
在我的 WPF 应用程序中,我试图根据用户选择的选项更改按钮的可见性。在加载时,我希望其中一个按钮不可见。我正在使用内置值转换器 BooleanToVisibilityConverter。但是它不起作用,因为按钮在加载时出现。我已将属性更改为真假,没有区别。下面是我的代码,我看不到我缺少什么?
我的视图模型中的属性
bool ButtCancel
{
get { return _buttCancel; }
set
{
_buttCancel = value;
OnPropertyChanged("ButtCancel");
}
}
在我的 app.xaml 中
<Application.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
在我的 MainWindow.xaml 中
<Button Grid.Column="2"
Command="{Binding CommandButtProgressCancel}"
Content="Cancel"
Visibility="{Binding ButtCancel, Converter={StaticResource BoolToVis}}"
IsEnabled="{Binding ButtCancelEnabled}"
Height="50" Width="120"
HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="0,0,50,20"/>
【问题讨论】:
-
您的
ButtCancel属性是public吗?它在您的代码摘录中没有访问修饰符,这将使它成为private,因此对绑定引擎不可见。此外,您不应该绑定IsEnabled属性;让按钮使用命令的CanExecute回调来确定自己的状态。 -
@MikeStrobel,当然默认访问修饰符是
internal而不是private。 -
是的,不敢相信我错过了!它不公开
-
@Sheridan 对于顶级声明,默认为
internal;对于类成员,包括嵌套类型声明,它是private。