0

Ich habe das folgende Verhalten, das die Farbformatierung eines GridControl festlegt, das funktioniert, wenn ich ein statisches ColorScaleFormat einstelle. Ich muss es jedoch an mein Ansichtsmodell binden, da das Farbskalenformat von den Modelldaten abhängt.Hinzufügen der Abhängigkeitseigenschaft zu einem WPF-Verhalten

Wie auch immer, ich muss es wie unten beschrieben zu einer DependencyProperty machen. Das Problem ist, dass ich zur Laufzeit den folgenden Fehler erhalte: Eine 'Bindung' kann nicht für die Eigenschaft 'ColorScaleFormat' vom Typ 'DynamicConditionBehavior' festgelegt werden. Eine 'Bindung' kann nur für eine DependencyProperty eines DependencyObject festgelegt werden.

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

Meine Ansicht Modell ist wie folgt:

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

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

Wenn ich die folgende Zeile in Verhalten ändern

Format = ColorScaleFormat 

zu

Format = defaultColorScaleFormat 

und entfernen Sie die Datenbindung aus der XAML alles funktioniert, aber ich möchte, um herauszufinden, wie ColorScaleFormat zu meinem Viewmodel Databind so kann ich es ändern, wenn sich die Daten ändern, indem Sie diese eine Eigenschaft zu schaffen.

Wie kann ich mein DynamicConditionBehavior implementieren DependencyObject und damit das ColorScaleFormat Databinded ermöglichen?

edit: Ich fand diese Klasse, die aber helfen kann ich nicht sicher bin, ob es in meinem Fall http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/

edit2 erforderlich ist: In der Zeit ist, ich habe gearbeitet, um das Problem, indem sie ein ITable2DView Schnittstelle, Übergeben einer Referenz der Ansicht zurück an das ViewModel. Das View-Modell ruft dann eine Funktion namens SetColourFormatter auf und übergibt die Variablen an das Verhalten, das in Ordnung ist. Ich bin immer noch neugierig, ob das obige möglich ist. Es scheint derzeit, als ob es nicht ist.

+0

Sie den Typ 'Binding' von' DynamicConditionBehavior.ColorScaleFormat' ändern müssen TypeOf Verhalten sein, dann mit dem 'Binding' etwas zu tun. – AnjumSKhan

+0

Funktioniert nicht, da Behavior dependencyObject nicht implementiert. Ich denke nicht, dass es tatsächlich möglich ist, aber ich löste es auf andere Weise – rolls

+0

Eg; 'public static schreibgeschützt DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register ( " ColorScaleFormat ", typeof (Binding), typeof (ColorScaleFormatProperty), neue FrameworkPropertyMetadata (defaultColorScaleFormat));' – AnjumSKhan

Antwort

0

sollte der DP

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

Ich werde das versuchen und zurückmelden. Vielen Dank. – rolls

Verwandte Themen