2015-07-28 60 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

是的聽起來不太好。瞭解一些示例應用程序如何組合在一起 –

相關問題