I'm generating a xaml from the code behind of a WPF application and want to add styles to the generated xaml. I'm using a resource dictionary to store the styling info which my app will access and apply to the appropriate elements in the generated xaml based on keys in the resource dictionary. Now I want to apply a specific style to all textboxes in the generated xaml. What I'm trying to do is to create this style in the resource dictionary without a key and I want to add this style resource to the resources of the generated xaml. I can't seem to find a way to add a particular resource to the resource dictionary of this generated xaml without a key. My questions is, is it possible to do so, i.e. add a resource to the resource dictionary of a usercontrol (or window etc.) without a key for that resource?
For example, here is the XAML I want to generate:
<UserControl>
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}">
<!--The TextBox style-->
</Style>
</UserControl.Resources>
</UserControl>
Thanks!
So, I found a way to do this. While adding to the resources of the generated xaml instead of specifying a string key, I used typeof(TextBox) and that seemed to work.
UserControl.Resources.Add(typeof(TextBox), styleDictionary["TextBoxStyleKey"]);
This results in:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="{x:Type TextBox}">
<!--The Style-->
</Style>
</UserControl.Resources>
Is there a better way(or any other way) of doing this?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With