[英]How to bind a control's property to another control's property?
I want that the SaveButton from my form to dissapear when the form is disabled. 我希望表单中的SaveButton在禁用表单时消失。 I do that this way:
我这样做:
this.formStackPanel.IsEnabled = someValue;
if(this.formStackPanel.IsEnabled)
{
this.saveButton.Visibility = Visibility.Visible;
}
else
{
this.saveButton.Visibility = Visibility.Collapsed;
}
Isn't there a way of binding those properties in the XAML? 是不是有办法在XAML中绑定这些属性? Is there a better way of doing that?
有没有更好的方法呢?
Yes. 是。 You should be able to bind the stackpanel's IsEnabled to your button's Visibility property.
您应该能够将stackpanel的IsEnabled绑定到按钮的Visibility属性。 However, you need a converter.
但是,您需要一个转换器。 WPF comes with a BooleanToVisibilityConverter class that should do the job.
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.