I have several controls that I implement how you describe, and yes I didn't want to clutter up my generic.xaml either. So I created a seperate xaml file for ecah control as a style and applied them in the control constructor, something like this:
And in the constructor, read the xaml in and apply the style to the control:
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourNameSpace;component/YourButtonStyle.xaml", UriKind.Relative));
StreamReader sr = new StreamReader(sri.Stream);
this.Style = (Style)XamlReader.Load(sr.ReadToEnd());
Rjacobs
Contributor
3460 Points
564 Posts
Re: Generic.xaml vs MyCustomControl.xaml
Sep 30, 2009 12:47 PM | LINK
I have several controls that I implement how you describe, and yes I didn't want to clutter up my generic.xaml either. So I created a seperate xaml file for ecah control as a style and applied them in the control constructor, something like this:
xaml:
<Style x:Name="MyButtonStyle" TargetType="Button"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="Padding" Value="3"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="White" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<BlahBlahBlah></BlahBlahBlah>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And in the constructor, read the xaml in and apply the style to the control:
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourNameSpace;component/YourButtonStyle.xaml", UriKind.Relative));
StreamReader sr = new StreamReader(sri.Stream);
this.Style = (Style)XamlReader.Load(sr.ReadToEnd());
Rob Jacobs