0

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.

rollsch
  • 2,518
  • 4
  • 39
  • 65
  • You have to change the type of `DynamicConditionBehavior.ColorScaleFormat` to `Binding` , then do something with the `Binding`. – AnjumSKhan Nov 18 '16 at 04:12
  • Doesn't work as Behavior does not implement dependencyObject. I do not think it is actually possible however I solved it another way – rollsch Nov 18 '16 at 04:43
  • Eg; `public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register( "ColorScaleFormat", typeof(Binding), typeof(ColorScaleFormatProperty), new FrameworkPropertyMetadata(defaultColorScaleFormat));` – AnjumSKhan Nov 18 '16 at 05:48

1 Answers1

1

the DP should be typeof Behavior

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

TeaDrivenDev
  • 6,591
  • 33
  • 50
blindmeis
  • 22,175
  • 7
  • 55
  • 74