向WPF行为添加依赖属性
本文关键字:依赖 属性 添加 WPF | 更新日期: 2023-09-27 18:18:26
我有以下行为,设置GridControl的颜色格式,如果我设置一个静态ColorScaleFormat,它可以很好地工作。然而,我需要将其绑定到我的视图模型,因为颜色比例格式取决于模型数据。
无论如何要做到这一点,我需要使它成为一个DependencyProperty如下所示。问题是我在运行时得到以下错误:'Binding'不能在'DynamicConditionBehavior'类型的'ColorScaleFormat'属性上设置。"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-not-a-dependency-object/
edit2:目前,我通过制作ITable2DView接口来解决问题,将视图的引用传递给ViewModel。视图模型然后调用一个名为setcolorformatter的函数,并将变量传递回正常工作的behavior。我仍然好奇上述情况是否可行。
DP应为typeof Behavior
public static readonly DependencyProperty ColorScaleFormatProperty =
DependencyProperty.Register(
nameof(ColorScaleFormat),
typeof(ColorScaleFormat),
typeof(DynamicConditionBehavior),
new FrameworkPropertyMetadata(defaultColorScaleFormat));