将依赖项属性添加到WPF行为
我有以下行为,设置GridControl的颜色格式,如果我设置静态ColorScaleFormat,它可以正常工作。然而,我需要将其数据绑定到我的视图模型,因为色彩格式取决于模型数据。将依赖项属性添加到WPF行为
无论如何,我需要使它成为一个DependencyProperty,如下所示。问题是我在运行时出现以下错误: 无法在'DynamicConditionBehavior'类型的'ColorScaleFormat'属性上设置'绑定'。 '绑定'只能在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接口工作,将View的引用传递回ViewModel。 View模型然后调用一个名为SetColourFormatter的函数,并将变量传递回行为正常的行为。尽管如此,我仍然很好奇。目前看起来好像不是。
的DP应的typeof行为
public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register(
"ColorScaleFormat", typeof(ColorScaleFormat), typeof(DynamicConditionBehavior), new FrameworkPropertyMetadata(defaultColorScaleFormat));
我会尝试并报告回来。谢谢。 – rolls
你必须DynamicConditionBehavior.ColorScaleFormat'的'类型更改为'Binding',然后做用'Binding'东西。 – AnjumSKhan
行为没有实现dependencyObject时不起作用。我不认为它实际上是可能的,但我解决了另一种方式 – rolls
例如; public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register( “ColorScaleFormat”,typeof(Binding),typeof(ColorScaleFormatProperty),new FrameworkPropertyMetadata(defaultColorScaleFormat));' – AnjumSKhan