【发布时间】:2015-07-28 12:07:11
【问题描述】:
我的老板下载了一个我必须在应用程序中使用的 xaml 控件。看起来不错,但内部逻辑有个奇怪的问题——
属性CurrentColor(我需要在我们的应用程序中使用)在control.xaml.cs 文件中定义,例如:
public SolidColorBrush CurrentColor
{
get { return (SolidColorBrush)GetValue(CurrentColorProperty); }
set
{
SetValue(CurrentColorProperty, value);
ActiveColor = CurrentColor;
}
}
我在我的对话框中使用这个控件(它有自己的视图模型类)和 我正在写这种绑定:
CurrentColor="{Binding myOwnViewModel.ColorActualValue, Mode=Default, UpdateSourceTrigger=PropertyChanged}">
在myOwnViewModel.cs(实现INotifyPropertyChanged)我有我的财产
public SolidColorBrush ColorActualValue{ // here is some logic}
但是当我调试一个应用程序时,我从来没有针对我的 CurrentColor - 我总是从control.xaml.cs 转到CurrentColor
如何将这个“第三方”控件从我的ViewModel 绑定到我的属性?
也许这是因为 (control.xaml.cs):
public static DependencyProperty CurrentColorProperty =
DependencyProperty.Register("CurrentColor", typeof(SolidColorBrush), typeof(ColorPickerComboBox), new PropertyMetadata(Brushes.Chocolate));
public static RoutedEvent ActiveColorChangedEvent = EventManager.RegisterRoutedEvent("ActiveColorChanged",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColorPickerComboBox));
我找到了问题 What's wrong with "DataContext = this" in WPF user controls? 并从该控件的构造函数中删除了 DataContext = this; - 但这仍然没有帮助
【问题讨论】:
-
将数据上下文设置为您的视图模型,然后从您的绑定声明中删除视图模型
-
您绑定到属性 myOwnViewModel.ColorActualValue 但在您的 ViewModel 中它命名为 CurrentColor。检查输出窗口是否存在绑定错误。
-
GreenEyedAndy - 抱歉这个错误我已经更正了问题
-
您能否提供一些指向控制项目/站点/完整代码的链接?
-
你的
ColorPickerComboBox set, and what is it? The binding ofmyOwnViewModel.ColorActualValue 的 DataContext 怎么样?看起来很奇怪,因为这意味着你绑定到ColorPickerComboBox.DataContext.myOwnViewModel.ColorActualValue,我怀疑myOwnViewModel要么是错误,要么是不公开的。您很可能正在尝试绑定到ColorPickerComboBox.DataContext.ColorActualValue,其中 DataContext 为myOwnViewModel。如果是这种情况,您的绑定应该只是{Binding ColorActualValue}