【问题标题】:Groupbox control IsEnabled state not propagated to child button when that button's IsEnabled is bound to some view model当按钮的 IsEnabled 绑定到某个视图模型时,Groupbox 控件 IsEnabled 状态不会传播到子按钮
【发布时间】:2014-07-21 21:55:55
【问题描述】:

Groupbox 包含一个包含两个按钮的 Grid。

希望代码能更好的表达:

<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");
        }
    }

...}

当我切换组框的启用状态时,我希望设置 IsEnabled 属性,但它根本没有被调用,并且绑定按钮也不会采用组框的启用状态。

有没有办法让(双向)绑定和父子关系同时工作?

【问题讨论】:

  • 您的要求不明确。您是否想要在禁用 GroupBox 时,也应禁用绑定按钮,反之亦然?
  • 是的,当切换组框时,我希望按钮启用的状态与组框启用匹配,并且仍然允许在按钮的视图模型中绑定到 IsEnabled。

标签: c# wpf groupbox isenabled


【解决方案1】:

使用它来将按钮的属性绑定到组框的属性

<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>

注意:IsEnabled="{Binding IsEnabled, ElementName=_groupBox}"

其次,没有已知的方法可以将按钮的属性绑定到同时匹配 _groupBox 和 viewmodel,因为它们都不同。即_groupBox.IsEnabled is falseviewmodel.IsEnabled is true 但是计算属性可以帮助您执行这样的逻辑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多