简体   繁体   中英

Add dependency property to a WPF behaviour

I have the following behavior that sets the color formatting of a GridControl which works fine if I set a static ColorScaleFormat. I however need to databind it to my view model as the colorscale format depends on the model data.

Anyway to do so I need to make it a DependencyProperty as done below. The issue is I get the following error at run time: A 'Binding' cannot be set on the 'ColorScaleFormat' property of type 'DynamicConditionBehavior'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

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,  
            });
        }
    }
}

My view model is as follows:

[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);
    }
}

and my Table2DView XAML code:

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

If I change the following line in behavior

Format = ColorScaleFormat

To

Format = defaultColorScaleFormat

And remove the databinding from the XAML everything works, however I want to figure out how to databind ColorScaleFormat to my ViewModel so I can change it when the data changes by creating this a property.

How can I make my DynamicConditionBehavior implement DependencyObject and hence allow the ColorScaleFormat to be databinded?

edit: I found this class which may help however I'm not sure if it is required in my case http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/

edit2: In the time being I have worked around the problem by making an ITable2DView interface, passing a reference of the View back to the ViewModel. The View model then calls a function called SetColourFormatter and passes the variables back to the Behaviour which works ok. I'm still curious if the above is possible though. It currently appears as if it is not.

the DP should be typeof Behavior

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM