質問

In my wpf application i have extended the datatrigger class as below

public class DescriptiveTrigger : System.Windows.DataTrigger
{
    public DescriptiveTrigger()
    {
    }

    private String _Description = "";
    public String Description
    {
        get { return _Description; }
        set { this._Description = value; }
    }
}

This is so the user can add a description to the triggers they create ( using a simple context menu to add and remove them). the xaml is then exported and saved to file using xamlwriter when it comes to reloading the file using xamlreader i get this

'MyControls.DescriptiveTrigger' trigger type in Style not recognized.  Error at object 'System.Windows.Style', Line 283 Position 22.

I have a version of this application written in .net4 that works correctly even with files created using the .net 3.5 version, so i know the saves are good.

Here is a sample of whats produced minus the non important parts

         <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> 

How can i fix this? and no i cant just use the .net4 version :P

役に立ちましたか?

解決

It seems like the trigger Collection do not accept custom triggers in .NET 3.5 (it's actually working in 4.0)

However, you don't need to inherit the DataTrigger class if you just want to add a Description property. That's what Attached Properties are for :)

Define this attached property in your 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));

And simply set it in you xaml :

<DataTrigger dc:Control1.Description="Background Color Trigger Where Value Is Greater Than 100">
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top