2012-07-18 72 views
0

我有ComboBox,其中我從我的「設置」對象與UserTypes ObservableCollection裏面我的ItemsSource,我需要將其SelectedItem綁定到我的「Employee」對象的類型屬性。窗口中還有其他字段,它們正確地綁定到「Employee」對象中的其他屬性,ItemsSource在Settings中正確填充了UserTypes屬性,唯一不起作用的是不與Combo綁定的type屬性'x selecteditem,可能是因爲它的datacontext被設置爲「Settings」對象。以下是我迄今爲止:ComboBox ItemsSource從SelectedItem中有不同的datacontext

<CollectionViewSource x:Key="settingsViewSource" d:DesignSource="{d:DesignInstance my:Settings, CreateList=True}" /> 
<CollectionViewSource x:Key="settingsUserTypesViewSource" Source="{Binding Path=UserTypes, Source={StaticResource settingsViewSource}}" /> 

<ComboBox DataContext="{StaticResource settingsUserTypesViewSource}" Name="userTypeBox" VerticalAlignment="Top" Width="100" Margin="2" ItemsSource="{Binding}" 
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"> 

在後面的代碼,我有:

 InitializeComponent(); 
     this.emp = emp; 
     InfoTab.DataContext = emp; 
     userTypeBox.DataContext = MainWindow.ViewSettings.UserTypes; 

EMP是綁定到正確的領域特定員工,只有組合框將selectedItem不工作。任何幫助將不勝感激。

+0

只是一個想法.. 。你的對象是否有「Type」屬性?嘗試更改該名稱,因爲已經存在「System.Type」。 – NestorArturo 2012-07-18 20:57:50

+0

,根本沒有幫助,沒有問題之前 – Erika 2012-07-18 21:10:38

+0

請發佈您的對應的ViewModel代碼,其中有界的屬性駐留... – 2012-07-18 21:26:47

回答

0

我不知道如果你在實際的代碼輸入錯誤,如果它只是在這裏,但你缺少一個支架

SelectedItem="Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 

應該

SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
+0

我改變了它,但它仍然不起作用 – Erika 2012-07-18 21:29:24

+0

你的調試輸出打印綁定錯誤?它應該沿着「System.Windows.Data錯誤:40:BindingExpression路徑錯誤:」 – LadderLogic 2012-07-18 21:36:47

+0

System.Windows.Data錯誤:40:BindingExpression路徑錯誤:'對象'上找不到'UserType'屬性'''ObservableCollection' 1'(HashCode = 50419575)'。 BindingExpression:路徑=用戶類型; DataItem ='ObservableCollection'1'(HashCode = 50419575);目標元素是'ComboBox'(Name ='userTypeBox');目標屬性是'SelectedItem'(類型'對象') – Erika 2012-07-18 21:43:48