I have many different parts to a panel I am using in my application. When a button is pressed I want to put certain parts of a panel/Canvas/Usercontrol etc to go an Edit Mode while I can excluse others based on an attached property i created. In my ViewModel I am using a boolean Property "IsEnabled" on which i bind a global style (Edit: Defined in a Resource Dictionary for Textboxes in this Example) as you can see below:
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled}" Value="False">
<Setter Property="Properties:Properties.Editable" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsEnabled}" Value="True">
<Setter Property="Properties:Properties.Editable" Value="True"/>
</DataTrigger>
<Trigger Property="Properties:Properties.Editable" Value="False">
<Setter Property="Background" Value="Red"/> //For visibility to see if its working
<Setter Property="IsReadOnly" Value="True"/>
</Trigger>
<Trigger Property="Properties:Properties.Editable" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="IsReadOnly" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
The attached Property is defined as:
static Properties()
{
EditableProperty = DependencyProperty.RegisterAttached("Editable", typeof(bool), typeof(EasyProperties), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
}
public static readonly DependencyProperty EditableProperty;
public static void SetEditable(DependencyObject dependencyObject, bool value)
{
dependencyObject.SetValue(EditableProperty, value);
}
public static bool GetEditable(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(EditableProperty);
}
an Example XAML would look like this where I want exclude a part of the userControl under a Stackpanel by setting the Editable Propertis in the Container to false:
<ScrollViewer Padding="5,5,5,5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Grid x:Name="_MainGrid" Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}" MinWidth="400">
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox x Width="40" Text="Test" Grid.Row="1"/>
<StackPanel Properties:Properties.Editable="False" Grid.Column="2" Grid.Row="1">
<TextBox Width="40" Text="Test" Grid.Column="2" Grid.Row="1"/>
<TextBox Width="40" Text="Test" Grid.Column="2" Grid.Row="1"/>
</StackPanel>
</Grid>
</Stackpanel>
What do I expect to happen:
To start withisEnabled Property of the ViewModel ist set to 'true' -> as such all Controls should be editable. But the Controls (Textboxed in this case only) under the Stackpanel should not be editable and with Red Background as the attached property was set to false in the Stackpanel they are a part of.
What happens:
The trigger does not work in the Controls under the Stackpanel and they are editable.
What I think is happening:
The Style is declaring the property for each Textbox again, which is distinct from the property they inherit from the Stackpanel. As such according to the Style the Controls should be enabled.
Can I adjust the trigger in the style in such a way that it would first look for inherited propertys and then look for the default I declare based on the IsEnabled property of my viewmodel? Is there maybe another way to do this which I am not seeing?
Edit: Clarified Title (I hope :D)