4

I am implementing an attached behavior in a WPF application. I need to pass a type parameters to the behavior, so I can call a method void NewRow(Table<T> table) on SqliteBoundRow. If I was instantiating an object in XAML, I would pass a type parameters using x:TypeArguments, but I don't see a way to do this when setting an attached behavior, because it uses a static property.

Code for the attached behavior looks like this:

public abstract class SqliteBoundRow<T> where T : SqliteBoundRow<T>
{
    public abstract void NewRow(Table<T> table);
}

public class DataGridBehavior<T> where T:SqliteBoundRow<T>
{
    public static readonly DependencyProperty IsEnabledProperty;
    static DataGridBehavior()
    {
        IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled",
            typeof(bool), typeof(DataGridBehavior<T>),
            new FrameworkPropertyMetadata(false, OnBehaviorEnabled));
    }
    public static void SetIsEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(IsEnabledProperty, value);
    }
    public static bool GetIsEnabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsEnabledProperty);
    }
    private static void OnBehaviorEnabled(DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs args)
    {
        var dg = dependencyObject as DataGrid;
        dg.InitializingNewItem += DataGrid_InitializingNewItem;
    }
    private static void DataGrid_InitializingNewItem(object sender,
        InitializingNewItemEventArgs e)
    {
        var table = (sender as DataGrid).ItemsSource as Table<T>;
        (e.NewItem as T).NewRow(table);
    }

}

XAML looks like this:

<DataGrid DataGridBehavior.IsEnabled="True">
    <!-- DataGridBehavior needs a type parameter -->
</DataGrid>

My current solution is to wrap DataGridBehavior in a a derived class which specifies the type parameters.

4

2 回答 2

6

最简单的解决方案是为您声明另一个Attached Property,但类型Type为为您保存参数值。在这种情况下,您将Type在您的之前设置属性IsEnabled Attached Property

<DataGrid DataGridBehavior.TypeParameter="{x:Type SomePrefix:SomeType}" 
    DataGridBehavior.IsEnabled="True" ... />

再次查看您的代码,似乎您的IsEnabled属性除了在表中添加新行之外什么都不做......在这种情况下,您没有理由不能用它替换它TypeParameter Attached Property并使用它来添加新行反而。

于 2014-06-02T15:36:00.940 回答
3

我不认为 WPF 提供了一种优雅的语法方式来做你想做的事。因此,我正要发布与 Sheridan 的答案类似的答案。也就是说,您可以提供类型 Type 的附加属性来确定泛型类型。然而,谢里登打败了我。下面是一些示例代码,说明如何使用反射来做到这一点:

Xaml

 <DataGrid behaviors:DataGridBehavior.InnerType="namespace:SqliteBoundRow"
      behaviors:DataGridBehavior.IsEnabled="True">
</DataGrid>

背后的代码

public abstract class DataGridBehavior
{
    private static readonly ConcurrentDictionary<Type, DataGridBehavior> Behaviors = new ConcurrentDictionary<Type, DataGridBehavior>();
    public static readonly DependencyProperty IsEnabledProperty;
    public static readonly DependencyProperty InnerTypeProperty;
    static DataGridBehavior()
    {
        IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled",
            typeof(bool), typeof(DataGridBehavior),
            new FrameworkPropertyMetadata(false, OnBehaviorEnabled));

        InnerTypeProperty = DependencyProperty.RegisterAttached("InnerType",
            typeof(Type), typeof(DataGridBehavior));
    }
    public static void SetIsEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(IsEnabledProperty, value);
    }
    public static bool GetIsEnabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsEnabledProperty);
    }

    public static void SetInnerType(DependencyObject obj, Type value)
    {
        obj.SetValue(InnerTypeProperty, value);
    }
    public static Type GetInnerType(DependencyObject obj)
    {
        return (Type)obj.GetValue(InnerTypeProperty);
    }

    private static void OnBehaviorEnabled(DependencyObject dependencyObject,
          DependencyPropertyChangedEventArgs args)
    {
        var innerType = GetInnerType(dependencyObject);
        if (innerType == null)
            throw new Exception("Missing inner type");

        var behavior = Behaviors.GetOrAdd(innerType, GetBehavior);
        behavior.OnEnabled(dependencyObject);
    }

    private static DataGridBehavior GetBehavior(Type innerType)
    {
        var behaviorType = typeof(DataGridBehavior<>).MakeGenericType(innerType);
        var behavior = (DataGridBehavior)Activator.CreateInstance(behaviorType);
        return behavior;
    }

    protected abstract void OnEnabled(DependencyObject dependencyObject);

}

public class DataGridBehavior<T> : DataGridBehavior
    where T : SqliteBoundRow
{
    protected override void OnEnabled(DependencyObject dependencyObject)
    {
        //dg.InitializingNewItem += DataGrid_InitializingNewItem;
    }

    private static void DataGrid_InitializingNewItem(object sender,
        InitializingNewItemEventArgs e)
    {
        //var table = (sender as DataGrid).ItemsSource as Table<T>;
        //(e.NewItem as T).NewRow(table);
    }

}

public class SqliteBoundRow
{
}
于 2014-06-02T18:37:20.457 回答