2011-01-21 127 views
0

我對WPF有一個相當簡單的問題。我已經四處搜索,但沒有找到「答案」呢......將ListBox.SelectedItem綁定到一個對象的屬性,並將ItemsSource綁定到另一個List對象(不同級別!)

我想做一個2級的數據綁定。簡化,我有一個人的一些屬性,也有N地址,每種類型和一些郵編(都可以從組合中選擇)。

我從服務器獲取以下數據:現在

public List<AddressType> AddressTypeList; // catalogue of Address Types 
public List<ZipCode> ZipCodeList; // catalogue of Zip Codes 

public Person Person; // Person object 
public List<Address> PersonAddressList; // Address List for that person (each has AddresType and ZipCode property) 

,我想這個數據綁定到一些控件。於是,我把這些屬性爲一些自定義的類(比如數據)和主要用戶控件的DataContext屬性設置爲這個對象:

data = new Data();    

data.AddressTypeList = dao.GetAll<AddressType>(); 
data.ZipCodeList = dao.GetAll<ZipCode>(); 
data.Person = dao.GetPerson(); 
data.PersonAddressList = dao.GetPersonAddressList(Person); 

this.DataContext = data; // this == some parent UserControl 

現在,我的地址列表XAML部分看起來是這樣的:

<ListBox Name="listBoxAddressList" ItemsSource="{Binding PersonAddressList}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <ComboBox ItemsSource="{Binding AddressTypeList}" SelectedItem="{Binding AddressType}"/> 
       <TextBox Text="{Binding Address}"/> <!-- this works! --> 
       <ComboBox ItemsSource="{Binding ZipCodeList}" SelectedItem="{Binding ZipCode}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

地址屬性獲取綁定確定,但AddressTypeList和ZipCodeList不包含。這是合乎邏輯的,因爲PersonAddress(在PersonAddressList中)沒有該屬性(它們處於不同的級別!)。我不想將AddressType和ZipCode的整個列表放入每個地址對象中,我希望將它無痛地綁定到「父」或將其傳遞給XAML中的每個地址列表項對象。

我認爲這是一個常見的要求,在一個組合(一個列表)中有一些值的目錄,但其中一個被選中,並且該值綁定到另一個對象的屬性。這將如何正確完成?

+0

我會爲PersonAddress項目創建一個具有必要屬性的ViewModel。或者你可以嘗試「{綁定路徑=(DataContext.AddressTypeList),RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ListBox}}}」,但我不確定這個解決方案,所以我不會發送是作爲答案。 – vorrtex 2011-01-21 09:26:57

+0

我已經遇到了這兩種可能的解決方案。我不喜歡第一個,因爲我會手動設置,然後在每個人的地址中攜帶所有可能的AddressType(以及ZipCodes等)......第二個選項(FindAncestor)似乎有點沉重(我猜想背景中有很多反射)。我正在尋找一個優雅的解決方案,但它似乎並不存在... – dabor 2011-01-21 10:21:11

回答

2

編輯2:您也可以嘗試設置您的列表作爲靜態屬性,這樣你也許能夠直接使用類似的綁定:

ItemsSource="{Binding Source={x:Static local:Data.AddressTypeList}}" 

編輯:嘗試使用RelativeSource返回原始數據,如下所示:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.AddressTypeList}" SelectedValue="{Binding AddressType}"/> 

另請參見:將SelectedValue綁定到屬性,而不是SelectedItem

例如

<ItemsControl ItemsSource="{Binding Data}" Grid.IsSharedSizeScope="True"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition SharedSizeGroup="A"/> 
        <ColumnDefinition SharedSizeGroup="B"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock Grid.Column="0" Text="{Binding Name}"/> 
       <ComboBox Grid.Column="1" SelectedValue="{Binding Occupation}"> 
        <sys:String>Programmer</sys:String> 
        <sys:String>GUI Designer</sys:String> 
        <sys:String>Coffee Getter</sys:String> 
       </ComboBox> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
相關問題