开发者

'Chained' WPF binding

I'm creating a UserControl in WPF, which I'm calling YesNoButton.

It has a single DependencyProperty, IsChecked, and contains two ToggleButtons, with labels "Yes" and "No". I've bound the IsChecked property of the the Yes ToggleButton to this IsChecked property of the parent YesNoButton. I've bound the IsChecked property of the No ToggleButton to the IsChecked property of the the Yes ToggleButton, through an 'inverse boolean' IValueConverter. Here's my YesNoButton markup, minus some of the look and feel frill:

<UserControl ...>
    <StackPanel Orientation="Horizontal">
        <ToggleButton x:Name="YesButton" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:YesNoButton}}, Path=IsChecked}">Yes</ToggleButton>
        <ToggleButton IsChecked="{Binding ElementName=YesButton, Path=IsChecked, Converter={StaticResource BooleanToInverseConverter}}">No</ToggleButton>
    </StackPanel>
</UserControl>

In my app, I include the YesNoButton as follows:

<local:YesNoButton IsChecked="{Binding Path=BoolPropertyOfDataContext}" />

When the window first loads, if I toggle the BoolPropertyOfDataContext by other means, the YesNoButton reacts perfectly.

But as soon as I click the Yes or No ToggleButton, al开发者_JAVA技巧though the YesNoButton visual state updates correctly, the binding to the BoolPropertyOfDataContext stops working altogether - it seems to be cancelled. I thought these 'binding chains' were supposed to work fine, but I'm obviously missing something here!

Any ideas?


Two suggestions:

  1. Name your UserControl so that you can refer to it
  2. Bind both ToggleButton IsChecked states to the IsChecked property for the UserControl

For example:

<StackPanel Orientation="Horizontal">
    <ToggleButton IsChecked="{Binding ElementName=YesNoButton, Path=IsChecked}">Yes</ToggleButton>
    <ToggleButton IsChecked="{Binding ElementName=YesNoButton, Path=IsChecked, Converter={StaticResource BooleanToInverseConverter}}">No</ToggleButton>
</StackPanel>


I got it working by styling a CheckBox instead:

<CheckBox IsChecked="{Binding Path=BoolPropertyOfDataContext}" Style="{StaticResource YesNoButton}" />

<Style x:Key="YesNoButton" TargetType="{x:Type CheckBox}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">

                <StackPanel Orientation="Horizontal">
                    <ToggleButton x:Name="YesButton" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type CheckBox}}, Path=IsChecked}">Yes</ToggleButton>
                    <ToggleButton IsChecked="{Binding ElementName=YesButton, Path=IsChecked, Converter={StaticResource BooleanToInverseConverter}}">No</ToggleButton>
                </StackPanel>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜