且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将控件的属性绑定到另一个控件的属性?

更新时间:2022-05-02 18:00:25

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