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