I have a user-control and I want to use it in some other project. There is no problem when I set some value to its properties directly:
<local:MyUserControl prop1="val1" prop2="val2">
...
</local:MyUserControl>
But I can’t apply a style to it. I tried:
<Window ...>
<Window.Resources>
<Style x:Key="MyUserControlStyle" TargetType="{x:Type local:MyUserControl}">
<Setter Property="prop1" Value="val1"/>
<Setter Property="prop2" Value="val2"/>
</Style>
</Window.Resources>
<Grid>
<local:MyUserControl Style="{StaticResource ResourceKey=MyUserControlStyle}">
...
</local:MyUserControl>
</Grid>
</Window>
Where did I wrong? -Thanks
Using dear @Mario Vernari’s instructions, I found it out that the problem was due to a bad strategy which I’d used to create my UserControl. I wanted to create a UserControl that be able to hold some other ones. So I had tried this:
Where
DP1is a dependency property of typeBrush. The UserControl which has been created through this way works if you set its properties (likeDP1) directly. Absolutely this is not the true way as @Mario told me:And he added:
Obviously, in this case we need to derive our lookless control from
ContentControlclass (instead ofControlclass). You may take a look at this & this to master the details.Here, I give thanks to @Mario again. 😉