wpf - XamlReader .net 3.5 'trigger type in Style not recognized'

标签 wpf xaml datatrigger xamlreader

在我的 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/

相关文章:

c# - 重新排序数据网格的列后如何获得新的列顺序?

c# - WPF & MVVM - 单元测试和弹出消息框或对话框

wpf - 如何将 XmlDataProvider.Source 绑定(bind)到 MVVM 属性

c# - 如何使取消按钮像 "X"按钮一样工作?

wpf - 使用 SourcName 在其他元素上触发

wpf - 如何将 DateTime 的默认值设置为空字符串?

wpf - ComboBox周围出现神秘的红色边框

wpf - WPF 超链接中的文本换行

c# - 如果 WPF 中没有子项,则隐藏 Expander ToggleButton

wpf datatrigger看不到用户控件属性