简体   繁体   English

当该按钮的IsEnabled绑定到某些视图模型时,Groupbox控件的IsEnabled状态不会传播到该子按钮

[英]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. Groupbox拥有一个网格,其中包含两个按钮。

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. 当我切换组框的启用状态时,我期望设置IsEnabled属性,但根本不会调用它,并且绑定按钮也不会采用组框的启用状态。

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的属性

<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}" 注意: 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. 其次,没有已知的方法来绑定按钮的属性以同时匹配_groupBox和viewmodel。 ie _groupBox.IsEnabled is false and viewmodel.IsEnabled is true however a calculated property can help you perform such logic _groupBox.IsEnabled is falseviewmodel.IsEnabled is true但是计算出的属性可以帮助您执行这种逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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