【问题标题】:Add dependency property to a WPF behaviour将依赖属性添加到 WPF 行为
【发布时间】:2017-04-01 19:58:15
【问题描述】:

我有以下行为来设置 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-not-a-dependency-object/

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

【问题讨论】:

  • 你必须把DynamicConditionBehavior.ColorScaleFormat的类型改成Binding,然后用Binding做点什么。
  • 不起作用,因为 Behavior 没有实现依赖对象。我认为这实际上是不可能的,但是我用另一种方式解决了它
  • 例如; public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register( "ColorScaleFormat", typeof(Binding), typeof(ColorScaleFormatProperty), new FrameworkPropertyMetadata(defaultColorScaleFormat));

标签: c# wpf data-binding dependency-properties dependencyobject


【解决方案1】:

DP 应该是 typeof Behavior

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

【讨论】:

  • 我会尝试并报告。谢谢。
猜你喜欢
  • 2012-12-25
  • 2014-07-29
  • 2020-12-22
  • 2015-05-27
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2014-05-23
相关资源
最近更新 更多