Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

c# - 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. 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? Is there a better way of doing that?

question from:https://stackoverflow.com/questions/9586956/how-to-bind-a-controls-property-to-another-controls-property

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...