0

我有以下行为来设置 GridControl 的颜色格式,如果我设置静态 ColorScaleFormat 效果很好。但是,我需要将它数据绑定到我的视图模型,因为色阶格式取决于模型数据。

无论如何,我需要将其设为 DependencyProperty,如下所示。问题是我在运行时收到以下错误:无法在“DynamicConditionBehavior”类型的“ColorScaleFormat”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

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-不是依赖对象/

edit2:暂时我通过制作 ITable2DView 接口解决了这个问题,将 View 的引用传递回 ViewModel。然后 View 模型调用一个名为 SetColourFormatter 的函数并将变量传递回正常工作的 Behavior。我仍然很好奇上述是否可行。它目前看起来好像不是。

4

1 回答 1

1

the DP should be typeof Behavior

public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register(
    "ColorScaleFormat", typeof(ColorScaleFormat), typeof(DynamicConditionBehavior), new FrameworkPropertyMetadata(defaultColorScaleFormat));
于 2016-11-18T08:40:41.743 回答