0

静的なColorScaleFormatを設定した場合にうまく動作するGridControlのカラーフォーマットを設定する次のような動作があります。しかし私は、カラースケールの形式はモデルデータに依存しているので、ビューモデルにデータバインドする必要があります。WPFの動作に依存プロパティを追加します。

これを行うには、次のようにDependencyPropertyにする必要があります。問題は、実行時に次のエラーが発生することです。 'DynamicConditionBehavior'タイプの 'ColorScaleFormat'プロパティで 'Binding'を設定できません。 「Binding」は、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のすべての作品からデータバインディングを削除するには、しかし、私はので、私はそれを変更することができ、私のViewModelにColorScaleFormatをデータバインドする方法を見つけ出すしたいときに、このプロパティを作成することによって、データの変更。

DynamicConditionBehaviorにDependencyObjectを実装させ、ColorScaleFormatをデータバインドできるようにするにはどうすればよいですか?

編集:、時間で私はITable2DViewインタフェースを行うことで問題を回避働いているされている:私はそれは http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/

EDIT2私の場合に必要とされている場合しかし、私はわからないんだけど助けて、このクラスを見つけましたビューの参照をViewModelに渡します。次に、ViewモデルはSetColourFormatterという関数を呼び出し、変数をOKの動作に戻します。私はまだ上記が可能であるかどうか不思議です。現在はそうでないかのように見えます。

+0

でなければなりません。 – AnjumSKhan

+0

BehaviorがdependencyObjectを実装していないため、動作しません。私はそれが実際に可能だとは思わないが、別の方法でそれを解決した – rolls

+0

例: 'Public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register( " ColorScaleFormat "、typeof(Binding)、typeof(ColorScaleFormatProperty)、新しいFrameworkPropertyMetadata(defaultColorScaleFormat)); ' – AnjumSKhan

答えて

0

DPは、あなたが `Binding`で何かをし、その後、Binding``へDynamicConditionBehavior.ColorScaleFormat` `の種類を変更する必要がtypeofを行動

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

私はそれを試みて報告します。ありがとうございました。 – rolls

関連する問題