Kev*_*v84 0 wpf binding converter datatemplate controltemplate
我目前停留在通过转换器为控件分配不同模板的问题上.
所以我有2个模板.
<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>
Run Code Online (Sandbox Code Playgroud)
我使用这个转换器控制这个:
<ControlA x:Name="_controlA" >
<Control Template="{Binding Converter={StaticResource templateConverters}}" />
</ControlA>
Run Code Online (Sandbox Code Playgroud)
我的转换器:
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;
}
Run Code Online (Sandbox Code Playgroud)
在我的转换器中,我如何能够引用我的xaml文件,以便它允许我为其分配我想要的模板?
谢谢和问候,凯夫
Sno*_*ear 10
也许您应该考虑其他一些实现,但这是您要求的:
您的转换器代码:
public class MyConverter : IValueConverter
{
public ControlTemplate TemplateA { get; set; }
public ControlTemplate TemplateB { get; set; }
... Convert methods using TemplateA and TemplateB properties...
}
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)