向WPF行为添加依赖项属性

inb24sb2  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(172)

我有以下行为来设置GridControl的颜色格式,如果我设置静态ColorScaleFormat,它可以正常工作。但是,我需要将它数据绑定到我的视图模型,因为色阶格式取决于模型数据。
无论如何,要这样做,我需要使它成为一个DependencyProperty,如下所示。问题是我在运行时得到以下错误:无法对类型为“DynamicConditionBehavior”的“ColorScaleFormat”属性设置“Binding”。只能对DependencyObject的DependencyProperty设置“Binding”。

public class DynamicConditionBehavior : Behavior<GridControl>
{
    GridControl Grid => AssociatedObject;

    protected override void OnAttached()
    {
        base.OnAttached();
        Grid.ItemsSourceChanged += OnItemsSourceChanged;
    }

    protected override void OnDetaching()
    {
        Grid.ItemsSourceChanged -= OnItemsSourceChanged;
        base.OnDetaching();
    }

    public ColorScaleFormat ColorScaleFormat {
        get { return (ColorScaleFormat) GetValue(ColorScaleFormatProperty); }
        set { SetValue(ColorScaleFormatProperty, value);}
    }
    public static ColorScaleFormat defaultColorScaleFormat = new ColorScaleFormat
    {
        ColorMin = (Color)ColorConverter.ConvertFromString("#FFF8696B"),
        ColorMiddle = (Color)ColorConverter.ConvertFromString("#FFFFEB84"),
        ColorMax = (Color)ColorConverter.ConvertFromString("#FF63BE7B")
    };

    public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register(
        "ColorScaleFormat", typeof(ColorScaleFormat), typeof(ColorScaleFormatProperty), new FrameworkPropertyMetadata(defaultColorScaleFormat));

    ....

    private void OnItemsSourceChanged(object sender, EventArgs e)
    {
        var view = Grid.View as TableView;
        if (view == null) return;

        view.FormatConditions.Clear();
        foreach (var col in Grid.Columns)
        {
            view.FormatConditions.Add(new ColorScaleFormatCondition
            {
                MinValue = 0,
                MaxValue = 20,
                FieldName = col.FieldName,
                Format = ColorScaleFormat,  
            });
        }
    }
}

我的视图模型如下:

[POCOViewModel]
public class Table2DViewModel
{
    public DataTable ItemsTable { get; set; }
    public ColorScaleFormat ColorScaleFormat { get; set; }
    public static Table2DViewModel Create(Table2D table2D)
    {
        var factory = ViewModelSource.Factory((Table2D table) => new Table2DViewModel(table));
        return factory(table2D);
    }
}

和我的Table2DView XAML代码:

<dxg:GridControl ItemsSource="{Binding ItemsTable}"
             AutoGenerateColumns="AddNew"
             EnableSmartColumnsGeneration="True">
<!--DesignTimeDataObjectType="{x:Type ViewModels:RowData}"-->

    <dxmvvm:Interaction.Behaviors >
        <behaviors:DynamicConditionBehavior ColorScaleFormat="{Binding ColorScaleFormat, Mode=OneWayToSource}" />
        </dxmvvm:Interaction.Behaviors>
        <dxg:GridControl.View>
            <dxg:TableView ShowGroupPanel="False"
                       AllowPerPixelScrolling="True"/>
        </dxg:GridControl.View>
    </dxg:GridControl>

如果我改变下面这行的行为

Format = ColorScaleFormat

Format = defaultColorScaleFormat

然后从XAML中删除数据绑定,一切正常,但是我想弄清楚如何将ColorScaleFormat数据绑定到我的ViewModel,这样我就可以通过创建这个属性在数据更改时更改它。
如何使DynamicConditionBehavior实现DependencyObject,从而允许ColorScaleFormat被数据绑定?
编辑:我发现这个类可能会有帮助,但是我不确定在我的http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/中是否需要它
edit2:目前我已经通过创建一个ITable2DView接口来解决这个问题,将View的引用传递回ViewModel。然后View模型调用一个名为SetColourFormatter的函数,并将变量传递回Behaviour,这样就可以正常工作了。不过我仍然很好奇上面的方法是否可行。目前看来似乎不可行。

mzsu5hc0

mzsu5hc01#

DP应为“行为类型”

public static readonly DependencyProperty ColorScaleFormatProperty =
    DependencyProperty.Register(
        nameof(ColorScaleFormat),
        typeof(ColorScaleFormat),
        typeof(DynamicConditionBehavior),
        new FrameworkPropertyMetadata(defaultColorScaleFormat));

相关问题