我希望这里有人可以帮助我。我很沮丧。
所以这是我的问题:
我有一个属性列表,这些属性是控件的属性。现在,我必须在属性网格和控件本身之间绑定。我的控件模板如下所示:
<DataTemplate x:Key="LabelVisualObject" DataType="{x:Type ContentControl}">
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource LabelLayoutTemplateSelector}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Properties, Converter={StaticResource PropertyConverter}, ConverterParameter=VisualizationObjectTypeAttribute.Layout.Name}" Value="Layout_OneLine">
<Setter Property="ContentTemplate" Value="{StaticResource LabelOneLineVisualObject}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Properties, Converter={StaticResource PropertyConverter}, ConverterParameter=VisualizationObjectTypeAttribute.Layout.Name}" Value="Layout_TwoLines">
<Setter Property="ContentTemplate" Value="{StaticResource LabelTwoLinesVisualObject}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
属性转换器
[ValueConversion(typeof(IEnumerable<IPropertyEditorAttribute>), typeof(object))]
public class PropertyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is IEnumerable<IPropertyEditorAttribute>)
{
IEnumerable<IPropertyEditorAttribute> list = value as IEnumerable<IPropertyEditorAttribute>;
foreach (IPropertyEditorAttribute cur in list)
{
if (cur.Name.Equals(parameter.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
return cur.Value;
}
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
好吧,我的机会是,当用户更改属性网格中的布局时,触发器应该做出反应并更改模板。
有谁知道,我怎么能做到这一点??
问候帕特里克
您可能正在寻找DataTemplateSelector
。
你可以在这里找到一个简短的教程:如何使用数据模板选择器
或者来自 MSDN 文档:
- 数据模板化概述
- 数据模板选择器类
您有一个设置ContentTemplate
的ContentTemplateSelector
,并且您尝试通过Style
设置ContentTemplate
。同时使用两者将不起作用,并且ContentTemplateSelector
可能优先作为本地值。