在我的 wpf 应用程序中,我扩展了 datatrigger 类,如下所示
public class DescriptiveTrigger : System.Windows.DataTrigger
{
public DescriptiveTrigger()
{
}
private String _Description = "";
public String Description
{
get { return _Description; }
set { this._Description = value; }
}
}
这样用户就可以向他们创建的触发器添加描述(使用简单的上下文菜单来添加和删除它们)。然后使用 xamlwriter 导出 xaml 并保存到文件,当使用 xamlreader 重新加载文件时,我得到了这个
'MyControls.DescriptiveTrigger' trigger type in Style not recognized. Error at object 'System.Windows.Style', Line 283 Position 22.
我有一个用 .net4 编写的应用程序版本,即使使用 .net 3.5 版本创建的文件也能正常工作,所以我知道保存很好。
这是减去非重要部分的产品样本
<Control1 xmlns="clr-namespace:MyControls.Controls;assembly=MyControls"
xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:dc="clr-namespace:MyControls;assembly=MyControls"
xmlns:dcc="clr-namespace:MyControls.Converters;assembly=MyControls"
xmlns:s="clr-namespace:System;assembly=mscorlib">
.........
<av:Label BorderBrush="#FF000000" BorderThickness="1,1,1,1" Background="#FF008000" Foreground="#FFFFFFFF" HorizontalAlignment="Left" VerticalAlignment="Center" ContextMenu="{av:DynamicResource LabelContextMenuKey}" av:Grid.Column="1">
<av:Label.Style>
<av:Style TargetType="av:IFrameworkInputElement">
<av:Style.Triggers>
<dc:DescriptiveTrigger Description="Background Color Trigger Where Value Is Greater Than 100">
<dc:DescriptiveTrigger.Binding>
<av:Binding Path="Content" ConverterParameter="100" RelativeSource="{av:RelativeSource Mode=Self}">
<av:Binding.Converter>
<dcc:GreaterThanToBooleanConverter />
</av:Binding.Converter>
</av:Binding>
</dc:DescriptiveTrigger.Binding>
<av:Setter Property="av:Panel.Background">
<av:Setter.Value>
<av:SolidColorBrush>#FF0000FF</av:SolidColorBrush>
</av:Setter.Value>
</av:Setter>
<dc:DescriptiveTrigger.Value>
<s:Boolean>True</s:Boolean>
</dc:DescriptiveTrigger.Value>
</dc:DescriptiveTrigger>
</av:Style.Triggers>
<av:Style.Resources>
<av:ResourceDictionary />
</av:Style.Resources>
</av:Style>
</av:Label.Style>000
</av:Label>
..........
</Control1>
我该如何解决这个问题?不,我不能只使用 .net4 版本 :P
最佳答案
似乎触发器集合不接受 .NET 3.5 中的自定义触发器(它实际上在 4.0 中工作)
但是,如果您只想添加 Description 属性,则无需继承 DataTrigger 类。这就是附加属性的作用:)
在您的 Control1.xaml.cs 中定义此附加属性:
public static string GetDescription(DependencyObject obj)
{
return (string)obj.GetValue(DescriptionProperty);
}
public static void SetDescription(DependencyObject obj, string value)
{
obj.SetValue(DescriptionProperty, value);
}
// Using a DependencyProperty as the backing store for Description. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.RegisterAttached("Description", typeof(string), typeof(Control1), new UIPropertyMetadata(string.Empty));
然后简单地在你的 xaml 中设置它:
<DataTrigger dc:Control1.Description="Background Color Trigger Where Value Is Greater Than 100">
关于wpf - XamlReader .net 3.5 'trigger type in Style not recognized',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372414/