2012-03-06 100 views
23

我希望表單中的SaveButton在表單被禁用時消失。 我這樣做,是這樣的:如何將控件的屬性綁定到另一個控件的屬性?

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

難道沒有綁定在XAML這些屬性的方法嗎?有沒有更好的方法來做到這一點?

回答

55

是的。你應該能夠將堆棧面板的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> 
+0

如果不是togglebutton,我有一個自定義控件(可以說CustomControl),它有一個togglebutton然後可以做同樣的事情,除了ElementName = CustomControl.togglebutton? – pasha 2016-10-05 14:02:43

相關問題