我希望表单中的SaveButton在禁用表单时消失。 我这样做:
this.formStackPanel.IsEnabled = someValue;
if(this.formStackPanel.IsEnabled)
{
this.saveButton.Visibility = Visibility.Visible;
}
else
{
this.saveButton.Visibility = Visibility.Collapsed;
}
是否有办法在XAML中绑定这些属性?有没有更好的方法呢?
答案 0 :(得分:66)
是。您应该能够将stackpanel的IsEnabled绑定到按钮的Visibility属性。但是,您需要一个转换器。 WPF附带了一个应该完成这项工作的BooleanToVisibilityConverter类。
<Window
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
<ToggleButton x:Name="toggleButton" Content="Toggle"/>
<TextBlock
Text="Some text"
Visibility="{Binding IsChecked, ElementName=toggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</Window>