Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could define 2 different <strong>Style</strong> properties in your custom control and <strong>bind them to the Style property of the individual buttons</strong>, thus allowing clients to set the style for the two controls independently of each other.</p> <p>XAML file:</p> <pre><code>&lt;UserControl x:Class="MyNamespace.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MyControl"&gt; &lt;StackPanel&gt; &lt;Button Name="FirstButton" Style={Binding ElementName=MyControl, Path=FirstButtonStyle} Content="First Button" /&gt; &lt;Button Name="SecondButton" Style={Binding ElementName=MyControl, Path=SecondButtonStyle} Content="Second Button" /&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>Code-behind file:</p> <pre><code>using System; using System.Windows; using System.Windows.Controls; namespace MyNamespace { [StyleTypedProperty( Property = "FirstButtonStyle", StyleTargetType = typeof(Button))] [StyleTypedProperty( Property = "SecondButtonStyle", StyleTargetType = typeof(Button))] public partial class MyControl : UserControl { public static readonly DependencyProperty FirstButtonStyleProperty = DependencyProperty.Register( "FirstButtonStyle", typeof(Style), typeof(MyControl) ); public Style FirstButtonStyle { get { return (Style)GetValue(FirstButtonStyleProperty); } set { SetValue(FirstButtonStyleProperty, value); } } public static readonly DependencyProperty SecondButtonStyleProperty = DependencyProperty.Register( "SecondButtonStyle", typeof(Style), typeof(MyControl) ); public Style SecondButtonStyle { get { return (Style)GetValue(SecondButtonStyleProperty); } set { SetValue(SecondButtonStyleProperty, value); } } } } </code></pre> <p>It is a good idea to implement these 2 properties as dependency properties, since that will make them conform to the other Style properties in standard WPF controls.</p> <p>Now you can set the style for the buttons like you would in any WPF control:</p> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:MyNamespace" Title="MyControl Sample" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="GreenButton" TargetType="{x:Type Button}"&gt; &lt;Setter Property="Background" Value="Green" /&gt; &lt;/Style&gt; &lt;Style x:Key="RedButton" TargetType="{x:Type Button}"&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;/Style&gt; &lt;/Windows.Resources&gt; &lt;StackPanel&gt; &lt;local:MyControl FirstButtonStyle="{StaticResource GreenButton}" SecondButtonStyle="{StaticResource RedButton}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Alternatively you could have the 2 buttons <strong>share the same Style by exposing a single property</strong> in your custom control which you bind to the Style property of both internal controls.</p> <p><strong>UPDATE</strong></p> <p>You don't have to define the <strong>FirstButtonStyle</strong> and <strong>SecondButtonStyle</strong> properties as dependency properties. The important thing is that the internal bindings to the buttons' <strong>Style</strong> properties are updated whenever their value changes. You can accomplish this by implementing the <strong>INotifyPropertyChanged</strong> interface in your user control and raise the <strong>OnPropertyChanged</strong> event in the property setters.</p> <p>You could also assign a "default style" to the 2 properties in the user control's constructor. Here is an example:</p> <pre><code>using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; namespace MyNamespace { [StyleTypedProperty( Property = "FirstButtonStyle", StyleTargetType = typeof(Button))] [StyleTypedProperty( Property = "SecondButtonStyle", StyleTargetType = typeof(Button))] public partial class MyControl : UserControl, INotifyPropertyChanged { private Style firstButtonStyle; private Style secondButtonStyle; public MyControl() { Style defaultStyle = new Style(); // assign property setters to customize the style this.FirstButtonStyle = defaultStyle; this.SecondButtonStyle = defaultStyle; } public Style FirstButtonStyle { get { return firstButtonStyle; } set { firstButtonStyle = value; OnPropertyChanged("FirstButtonStyle"); } } public Style SecondButtonStyle { get { return secondButtonStyle; } set { secondButtonStyle = value; OnPropertyChanged("SecondButtonStyle"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload