简体   繁体   中英

Groupbox control IsEnabled state not propagated to child button when that button's IsEnabled is bound to some view model

Groupbox holds a Grid which holds two buttons.

Hopefully the code can better express it:

<Window x:Class="PanelTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="344" Width="361"
    x:Name="This">
<Grid>
    <GroupBox x:Name="_groupBox" IsEnabled="False" Header="GroupBox" HorizontalAlignment="Left" Margin="30,87,0,0" VerticalAlignment="Top" Height="206" Width="300">
        <Grid>
            <Button Content="Bound Button" IsEnabled="{Binding IsEnabled, Mode=TwoWay}" HorizontalAlignment="Left" Height="51" VerticalAlignment="Top" Width="128" Margin="140,75,0,0"/>
            <Button Content="Unbound Button" HorizontalAlignment="Left" Margin="10,75,0,0" VerticalAlignment="Top" Width="125" Height="51"/>
        </Grid>
    </GroupBox>
    <Button Content="Toggle Group Enabled" HorizontalAlignment="Left" Margin="93,29,0,0" VerticalAlignment="Top" Width="161" Click="EnableClick" Height="35"/>

</Grid>

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        DataContext = new ButtonViewModel();
    }

    private void EnableClick(object sender, RoutedEventArgs e)
    {
        _groupBox.IsEnabled = !_groupBox.IsEnabled;
    }
}

public class ButtonViewModel : INotifyPropertyChanged {

    static ButtonViewModel() {
        eventArgCache = new Dictionary<string, PropertyChangedEventArgs>();
    }

    bool _enabled;
    public bool IsEnabled {
        get { return _enabled; }
        set {
            if (_enabled == value)
                return;
            _enabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

...}

When I toggle the enabled state of the groupbox I was expecting the IsEnabled property to be set, but it doesn't get called at all and the bound button also doesn't take the enabled state of the groupbox.

Is there a way to make both the (two-way) binding and the parent-child relationship work at the same time?

use this to bind button's property to groupbox's property

<GroupBox x:Name="_groupBox" IsEnabled="False" Header="GroupBox" HorizontalAlignment="Left" Margin="30,87,0,0" VerticalAlignment="Top" Height="206" Width="300">
    <Grid>
        <Button Content="Bound Button" IsEnabled="{Binding IsEnabled, ElementName=_groupBox}" HorizontalAlignment="Left" Height="51" VerticalAlignment="Top" Width="128" Margin="140,75,0,0"/>
        <Button Content="Unbound Button" HorizontalAlignment="Left" Margin="10,75,0,0" VerticalAlignment="Top" Width="125" Height="51"/>
    </Grid>
</GroupBox>

note: IsEnabled="{Binding IsEnabled, ElementName=_groupBox}"

secondly there is no known way to bind the button's property to match _groupBox & viewmodel at the same time when they both differs. ie _groupBox.IsEnabled is false and viewmodel.IsEnabled is true however a calculated property can help you perform such logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM