簡體   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擁有一個網格,其中包含兩個按鈕。

希望代碼可以更好地表達它:

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

<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但是計算出的屬性可以幫助您執行這種邏輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM