如何将控件的属性绑定到另一个控件的属性? [英] How to bind a control's property to another control's property?

查看:92
本文介绍了如何将控件的属性绑定到另一个控件的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望禁用表单时消失我表单中的SaveButton. 我这样做是这样的:

I want that the SaveButton from my form to dissapear when the form is disabled. I do that this way:

this.formStackPanel.IsEnabled = someValue;
if(this.formStackPanel.IsEnabled)
{
    this.saveButton.Visibility = Visibility.Visible;
}
else
{
    this.saveButton.Visibility = Visibility.Collapsed;
}

在XAML中没有绑定这些属性的方法吗?有更好的方法吗?

Isn't there a way of binding those properties in the XAML? Is there a better way of doing that?

推荐答案

是.您应该能够将堆栈面板的IsEnabled绑定到按钮的Visibility属性.但是,您需要一个转换器. WPF带有一个BooleanToVisibilityConverter类,该类可以完成这项工作.

Yes. You should be able to bind the stackpanel's IsEnabled to your button's Visibility property. However, you need a converter. WPF comes with a BooleanToVisibilityConverter class that should do the job.

<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>

这篇关于如何将控件的属性绑定到另一个控件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆