XamlReader .net 3.5'样式中的触发器类型无法识别'

时间:2012-11-14 03:07:19

标签: 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; }
    }
}

这样,用户就可以为他们创建的触发器添加描述(使用简单的上下文菜单添加和删除它们)。然后在使用xamlreader重新加载文件时,使用xamlwriter将xaml导出并保存到文件中我得到了这个

'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

1 个答案:

答案 0 :(得分:1)

似乎触发器Collection不接受.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">