2010-04-23 69 views
2

我有一個包含一個文本框,並在其DataTemplate中的組合框的列表框:一個ListBox中綁定一個WPF組合框到不同的ItemsSource的DataTemplate

<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBox Text="{Binding Path=Name}" /> 
        <ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

我想列表框綁定到對象的集合(其我已經成功完成了),但我希望上述數據模板中的組合框的itemssource設置爲窗口上的本地屬性(int數組)。我仍然希望組合框在其所選項目和對象集合上的屬性之間具有雙向綁定...

我在代碼中有以下代碼: PitcherListBox.DataContext = this;

基本上,我希望列表框中的組合框具有與列表框本身不同的itemssource。我似乎無法弄清楚如何在XAML中更改組合框的ItemsSource。有人可以提供一些反饋嗎?謝謝!

回答

4

試試這個:

<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" /> 

請注意,您需要使用含有LocalArrayOfIntsProperty窗口的類型來代替YourWindowTypeHere!另外請記住,您將需要爲該類型定義一個xml命名空間!

+0

我創建了一個命名空間「xmlns:Local =」clr-namespace:MyApp「,並使用下面提供的行,使用這個類型:AncestorType = {x:Type Local:DetailsWindow},並且我得到一個錯誤:Ancestor type必須在FindAncestor模式中爲相對源指定 – tjans 2010-04-23 16:53:50

+0

Nevermind,我只需構建並且錯誤消失了。非常感謝您的幫助... – tjans 2010-04-23 17:00:47

+0

是的,這是VS2008中的一個錯誤(不知道VS2010)。你說,它通常在重建後消失BTW:如果我的答案有幫助,你應該點擊除了我的答案之外的複選標記,將其標記爲已接受。 – gehho 2010-04-26 06:17:21

相關問題