2015-02-11 37 views
0

我有一個這樣的組合框裏面DataGrid控件的一個領域:顯示數據網格,而不是整列

<ComboBox DisplayMemberPath="{Binding CodeGDP_Collection/gdp_code}" > 
         <ComboBoxItem > 
          <DataGrid ItemsSource="{Binding CodeGDP_Collection}" AutoGenerateColumns="False"> 
           <DataGrid.Columns> 
            <DataGridTextColumn Binding="{Binding gdp_code}" /> 
            <DataGridTextColumn Binding="{Binding gdp_nom}" /> 
            <DataGridTextColumn Binding="{x:Null}"/> 
            <DataGridTextColumn Binding="{Binding gdp_ville}" /> 
            <DataGridTextColumn Binding="{Binding gdp_code_postal}"/> 
           </DataGrid.Columns> 
          </DataGrid> 
         </ComboBoxItem> 
        </ComboBox> 

我想,當我選擇在數據網格行,只有一個字段會顯示在組合框中(例如gdp_code)。

在當前情況下,在每個選擇中顯示整列gdp_code

那麼我該如何解決?

回答

1

的selectedItem屬性綁定ComboboxItem爲了結合將selectedItem你將不得不提供的ItemsSource到您的組合框,這是不是在這種情況下是有益的。 您可以簡單地將combobox.text複製到您的值,即使它不在列表中,組合框也會顯示它。

您可以將選定的項目從網格綁定到屬性,然後將組合框文本設置爲選定的屬性值。

我之前做過這個,下面的代碼不是寫在設計器中,因此它可能是錯誤的,但它是給你一個想法。

<ComboBox 
DisplayMemberPath="DataContext.MySelectedItem.gdp_code" 
Text="{Binding MySelectedItem.gdp_code}" 
IsEditable="True"> 
<ComboBox.Items> 
    <ComboBoxItem> 
     <ComboBoxItem.Template> 
      <ControlTemplate> 
       <DataGrid SelectedItem={Binding MySelectedItem} ItemsSource="{Binding CodeGDP_Collection}" AutoGenerateColumns="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding gdp_code}" /> 
         <DataGridTextColumn Binding="{Binding gdp_nom}" /> 
         <DataGridTextColumn Binding="{x:Null}"/> 
         <DataGridTextColumn Binding="{Binding gdp_ville}" /> 
         <DataGridTextColumn Binding="{Binding gdp_code_postal}"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </ControlTemplate> 
     </ComboBoxItem.Template> 
    </ComboBoxItem> 
</ComboBox.Items> 

+1

而其整個....! – Muds 2015-02-11 12:44:14

0

你應該在數據網格

+0

u能解釋更多 – 2015-02-11 11:32:04