I'm new to wpf so I'm looking for a solution and an explanation of why my attempted solution is not working.
Here's my case. I have several UserControls. In each of them I apply the following style:
<UserControl.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource Centratura}">
<Setter Property="IsEnabled" Value="{Binding Property1.IsAbilitato}" />
</Style>
</UserControl.Resources>
based on a style defined in a resource dictionary. And it works fine. But note that for every UserControl, the previous code is identical, except for the binding property, that will be Property1.IsAbilitato, Property2.IsAbilitato, Property3.IsAbilitato...
But this is a code duplication I don't like. I was wondering how to put the style in the resource dictionary and apply the proper binding later in each usercontrol.
I tried using the Tag property like what was suggested here, in this way:
In my user control:
<UserControl x:Class="whatever"
...
Tag="{Binding Property1.IsAbilitato}"
...>
and in the resourcedictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBox">
<Setter Property="IsEnabled" Value="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</ResourceDictionary>
but it doesn't work. Suggestions? Other solutions? (I'm using MVVM, if it's relevant). Thanks in advance.