【问题标题】:How to bind a control's property to another control's property?如何将一个控件的属性绑定到另一个控件的属性?
【发布时间】:2012-03-24 02:58:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# .net wpf binding properties


    【解决方案1】:

    是的。您应该能够将堆栈面板的 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>
    

    【讨论】:

    • 如果不是切换按钮,我有一个自定义控件(比如说 CustomControl),它有一个切换按钮,那么除了 ElementName=CustomControl.togglebutton 之外可以做同样的事情?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2015-06-15
    • 2015-06-29
    • 2012-03-27
    • 2011-03-03
    相关资源
    最近更新 更多