Reputation: 175
I've a C#/WPF application.On my Mainwindow.xaml,I'm programmatically loading other views. There's a checbox on MainWindow.xaml.When its clicked, I need to make all textbox controls on the screen as readonly. The code is working for controls on Mainwindow but not for textboxes on AView.xaml
What am I missing here please?Or is there any other way of achieving this functionality?
Thanks.
Here's my code:
MainWindow.xaml:
<CheckBox IsChecked="{Binding IsCheckboxChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ContentPresenter Content="{Binding CurrentViewModel}" >
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type ViewModel:VModelA}" >
<Views:AView/>
</DataTemplate>
</ContentPresenter>
MainWindowResources.xaml:
<Style TargetType="TextBox" x:Key="ReadOnlyStyle">
<Setter Property="IsReadOnly" Value="{Binding Path=IsCheckboxChecked}"/>
</Style>
<Style TargetType="TextBox" x:Key="ReadOnlyStyleChild">
<Setter Property="IsReadOnly" Value="{Binding Path=IsCheckboxCheckedChild}"/>
</Style>
MainWindowViewModel.cs
private static MainWindowViewModel _instance = new MainWindowViewModel();
public static MainWindowViewModel Instance
{
get {
return _instance;
}
}
public bool IsCheckboxChecked
{
get {
return m_isCheckboxChecked;
}
set
{
m_isCheckboxChecked = value;
OnPropertyChanged("IsCheckboxChecked");
}
}
VModelA.cs:
public bool IsCheckboxCheckedChild
{
get {
return MainWindowViewModel.Instance.IsCheckboxChecked;
}
}
AView.xaml
<TextBox Style="{DynamicResource ReadOnlyStyleChild}">
Upvotes: 0
Views: 319
Reputation: 311315
If you look in the output window when debugging your app, I suspect you'll see messages about binding failures for this:
{Binding Path=IsCheckboxChecked}
Binding in styles is tricky to do. What is the source? You might need to specify ElementName
or RelativeSource
.
Upvotes: 1