Reputation: 4095
I have a CustomControl
with custom property (DependencyProperty
) which called "SingleRow".
If the property is set to true I need to use specific style.
If the property is set to false I need to use the default style - for that I dont need to do anything special.
here are my styles:
<Style TargetType="{x:Type local:MetroTabControl}">
...
</Style>
<Style x:Key="MetroTabControlSingleRow" TargetType="{x:Type local:MetroTabControl}">
...
</Style>
How can I set the CustomControl
to use the "MetroTabControlSingleRow" when the SingleRow property is true?
I tried:
public static DependencyProperty SingleRowPropertyKey = DependencyProperty.Register("SingleRow", typeof(bool), typeof(MetroTabControl), new PropertyMetadata(true));
public bool SingleRow
{
get { return (bool)GetValue(SingleRowPropertyKey); }
set { base.SetValue(SingleRowPropertyKey, value); }
}
public override void OnApplyTemplate()
{
if (SingleRow)
{
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/MetroControls;component/Generic.xaml", System.UriKind.Relative);
Resources.MergedDictionaries.Add(rd);
SetResourceReference(MetroTabControl.StyleProperty, "MetroTabControlSingleRow");
Style = (Style)this.FindResource("MetroTabControlSingleRow");
}
}
But it keep crashing.
Edit: Also, based on dev hedgehog's comment, I tried:
<Style TargetType="{x:Type local:MetroTabControl}">
<Style.Triggers>
<Trigger Property="SingleRow" Value="True">
<Setter Property="Template" Value="{StaticResource MetroTabControlSingleRow}" />
</Trigger>
<Trigger Property="SingleRow" Value="False">
<Setter Property="Template" Value="{StaticResource MetroTabControlMultiRows}" />
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="MetroTabControlSingleRow" TargetType="{x:Type local:MetroTabControl}">
...
</ControlTemplate>
<ControlTemplate x:Key="MetroTabControlMultiRows" TargetType="{x:Type local:MetroTabControl}">
...
</ControlTemplate>
and still crashes.
Would appreciate your help.
Upvotes: 2
Views: 4206
Reputation: 8791
Just use triggers for that.
Here is an example with IsFocused property.
<ControlTemplate x:Key="NotFocused" TargetType="{x:Type TextBox}">
. . .
</ControlTemplate>
<ControlTemplate x:Key="Focused" TargetType="{x:Type TextBox}">
. . .
</ControlTemplate>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Template" Value="{StaticResource Focused}" />
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Template" Value="{StaticResource NotFocused}" />
</Trigger>
</Style.Triggers>
</Style>
Upvotes: 3
Reputation: 1370
This is how I use it and it works for me.
Code:
public class MyConverter : IValueConverter
{
public DataTemplate First { get; set; }
public DataTemplate Second { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
return First;
else
return Second;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<DataTemplate x:Key="first">
<Button>one</Button>
</DataTemplate>
<DataTemplate x:Key="second">
<CheckBox/>
</DataTemplate>
<local:MyConverter x:Key="myConverter"
First="{StaticResource first}"
Second="{StaticResource second}" />
<Style TargetType="{x:Type local:MyCustomControl1}">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter ContentTemplate="{Binding SingleRow, Converter={StaticResource myConverter}, RelativeSource={RelativeSource TemplatedParent}}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Upvotes: 1