静的な ColorScaleFormat を設定すると正常に動作する GridControl の色の書式設定を設定する次の動作があります。ただし、カラースケール形式はモデル データに依存するため、ビュー モデルにデータバインドする必要があります。
とにかくそうするには、以下のように 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からデータバインディングを削除すると、すべてが機能しますが、ColorScaleFormatをViewModelにデータバインドする方法を理解したいので、これをプロパティに作成してデータが変更されたときに変更できます。
DynamicConditionBehavior に DependencyObject を実装させて、ColorScaleFormat をデータバインドできるようにするにはどうすればよいですか?
編集:役立つかもしれないこのクラスを見つけましたが、私の場合に必要かどうかはわかりません http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-非依存オブジェクト/
edit2:当面の間、ITable2DView インターフェイスを作成し、View の参照を ViewModel に戻すことで問題を回避しました。次に、View モデルは SetColourFormatter という関数を呼び出し、変数を正常に動作する Behavior に戻します。ただし、上記が可能かどうかはまだ興味があります。現在、そうではないかのように見えます。