开发者

WPF dependency between controls

I have 3 textboxes their visibility depends on checkbox. I want to set IsEnabled = false on all textboxes, when checkbox.IsChecked = false and IsEnabled = true when IsChecked=开发者_C百科true. How can I achieve this dependency in XAML?

thanks.


you can bind between named elements, and as long as you are binding to dependency properties, the binding will reflect any changes

<TextBox IsEnabled="{Binding ElementName=SomeCheckBox, Path=IsChecked}" />


<StackPanel>
    <CheckBox Name="Checker" />

    <TextBox IsEnabled="{Binding ElementName=Checker, Path=IsChecked}" />
    <TextBox IsEnabled="{Binding ElementName=Checker, Path=IsChecked}" />
    <TextBox IsEnabled="{Binding ElementName=Checker, Path=IsChecked}" />
</StackPanel>


Bind the IsEnabled property to the IsChecked property on the CheckBox.

<TextBox IsEnabled="{Binding ElementName=NameOfCheckBox, Path=IsChecked}" />

If your goal is to then tie that bool value to Visibility as your question suggests, you would then need to also leverage a converter, such as the BooleanToVisibilityConverter.

<TextBox Visibility="{Binding IsChecked, ElementName=NameOfCheckBox, Converter={StaticResource BoolToVisConverter}}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜