我有以下行为设置GridControl的颜色格式,如果我设置静态ColorScaleFormat,它可以正常工作。然而,我需要将它数据绑定到我的视图模型,因为色阶格式取决于模型数据。
无论如何要这样做我需要使它成为DependencyProperty,如下所示。问题是我在运行时遇到以下错误: 无法在“DynamicConditionBehavior”类型的“ColorScaleFormat”属性上设置“绑定”。 '绑定'只能在DependencyObject的DependencyProperty上设置。
public void textBoxTranspo_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}
public void textBoxDaily_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}
我的观点模型如下:
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,
});
}
}
}
和我的Table2DView XAML代码:
[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);
}
}
如果我在行为中更改以下行
<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
从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的函数,并将变量传递回行为正常的行为。如果上述情况可行,我仍然很好奇。它目前看起来好像不是。
答案 0 :(得分:1)
DP应该是行为类型
Exception in thread "main" MultiException stack 1 of 3
java.lang.IllegalArgumentException: Invalid injectee with required type of T passed to getInjecteeDescriptor
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetInjecteeDescriptor(ServiceLocatorImpl.java:546)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getInjecteeDescriptor(ServiceLocatorImpl.java:585)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:70)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:228)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
at com.github.fabriziocucci.test.Test.main(Test.java:39)
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.github.fabriziocucci.test.Test$A errors were found
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:246)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
at com.github.fabriziocucci.test.Test.main(Test.java:39)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on com.github.fabriziocucci.test.Test$A
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:386)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
at com.github.fabriziocucci.test.Test.main(Test.java:39)