[英]Binding visibility of rectangle control to button's isEnabled property
[英]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 false
和viewmodel.IsEnabled is true
但是计算出的属性可以帮助您执行这种逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.