I am currently stuck on a problem assigning different templates to a control via a converter.
So I have 2 templates.
<ControlTemplate x:Name="_templateA" x:Key="templateA">
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<Blah />
</StackPanel>
</ControlTemplate>
<ControlTemplate x:Name="_templateB" x:Key="templateB">
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<Blah Blah />
</StackPanel>
</ControlTemplate>
and I have this control using this converter:
<ControlA x:Name="_controlA" >
<Control Template="{Binding Converter={StaticResource templateConverters}}" />
</ControlA>
My Converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Object a;
ControlTemplate template = null;
try
{
a= value as ObjectA;
if (value != null)
template = a.useTemplate1 ? [templateA from xaml] : [templateB from xaml];
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
}
return toolbar;
}
In my Converter, how am I able to get reference to my xaml file so that it allows me to assign it my desired template???
Thanks and Regards, Kev
Maybe you should think on some other implementation but here is what you're asking for:
your converter code:
public class MyConverter : IValueConverter
{
public ControlTemplate TemplateA { get; set; }
public ControlTemplate TemplateB { get; set; }
... Convert methods using TemplateA and TemplateB properties...
}
usage in XAML:
<UserControl.Resources>
<!-- templates with 'templateA' and 'templateB' keys -->
<Converters:MyConverter x:Key="templateConverters" TemplateA="{StaticResource templateA}" TemplateB="{StaticResource templateB}" />
<UserControl.Resources>
...
<ControlA x:Name="_controlA" >
<Control Template="{Binding Converter={StaticResource templateConverters}}" />
</ControlA>