2015-07-28 89 views
0

我的老板已经下载了我必须在应用程序中使用的xaml控件。它看起来像确定,但有一个与内在逻辑奇怪的麻烦 - 该 财产CurrentColor(我需要在我们的应用程序使用)中control.xaml.cs文件等被定义为:将控件绑定到另一个属性

public SolidColorBrush CurrentColor 
{ 
    get { return (SolidColorBrush)GetValue(CurrentColorProperty); } 
    set 
    { 
      SetValue(CurrentColorProperty, value); 
      ActiveColor = CurrentColor; 
    } 
} 

我用我的对话框这个控制(有它自己的视图模型类)和 我写这样的结合:在myOwnViewModel.cs

CurrentColor="{Binding myOwnViewModel.ColorActualValue, Mode=Default, UpdateSourceTrigger=PropertyChanged}"> 

(实现INotifyPropertyChanged)我有我的财产

public SolidColorBrush ColorActualValue{ // here is some logic} 

但是当我调试应用程序,我从来没有定位我CurrentColor - 我经常去CurrentColorcontrol.xaml.cs

我怎么能结合从我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; - 但是这仍然没有帮助

+0

设置的DataContext到乌尔视图模型,然后从乌尔约束力的声明 –

+1

你删除视图模型视图模型类绑定到属性myOwnViewModel.ColorActualValue,但在您的ViewModel中它命名为CurrentColor。检查输出窗口是否有绑定错误。 – GreenEyedAndy

+0

GreenEyedAndy - 对于此错误感到抱歉,我已更正问题 – curiousity

回答

0

应结合看起来像这样?

CurrentColor="{Binding ColorActualValue, Mode=Default, UpdateSourceTrigger=PropertyChanged}"> 

你的“对话”的DataContext必须包含ColorActualValue财产

public Dialog() 
{ 
    DataContext = new myOwnViewModel(); 
} 
+0

不,我已经试过了。任何方式感谢您的答案,并看看我更新的问题 – curiousity

+0

我无法将myOwnViewModel的实例传递给Dialog构造函数 - 首先Dialog在另一个项目中,其次myOwnViewModel不是静态类,并且具有项目中不存在的参数Dialog所在的位置。我将尝试在myOwnViewModel可用的位置(在使用适当的DataContext将控件更改为另一个控件时)在InitializeComponent之后编写正确的代码,但我认为这是一个不好的练习 – curiousity

+0

是的听起来不太好。了解一些示例应用程序如何组合在一起 –

相关问题