2011-03-24 54 views
0

我想將一個ItemsSource綁定到RowDetailsTemplate中的組合框。如果我把一個ComboBox放在網格外面,它可以正常工作。我認爲這是發生的,因爲網格上的ItemsSource屬性可能會拋出RowDetailsTemplate中的ComboBox。 XAML低於任何想法?在DataGrid RowDetailsTemplate中綁定組合框

分類和CatType是兩個不同的ObservableCollections。

編輯:沒有發生錯誤ComboBox只是顯示爲空。

<ComboBox ItemsSource="{Binding CatTypes}"></ComboBox> 
     <my:DataGrid Name="gridProds" AutoGenerateColumns="False" 
     AlternatingRowBackground="Gainsboro" ItemsSource="{Binding Categories}"> 
      <my:DataGrid.Columns> 
       <my:DataGridTextColumn x:Name="CatId" Header="CatID" Width="Auto" Binding="{Binding CategoryID}" /> 
       <my:DataGridTextColumn Header="CatName" Width="Auto" Binding="{Binding CategoryName}" /> 
      </my:DataGrid.Columns> 
      <my:DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <Border> 
         <StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <Label>ID:</Label> 
           <TextBox Name="txtGridCatId" Text="{Binding CategoryID}"/> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <Label>Category Type:</Label> 
           <ComboBox ItemsSource="{Binding CatTypes}"></ComboBox> 
          </StackPanel> 
         </StackPanel> 
        </Border> 
       </DataTemplate> 
      </my:DataGrid.RowDetailsTemplate> 
     </my:DataGrid> 

有一個在數據源稱爲一類在下面做:

private ObservableCollection<string> _cattypes = new ObservableCollection<string> { }; 

    public ObservableCollection<string> CatTypes 
    { 
     get 
     { 

      _cattypes = new ObservableCollection<string> { }; 

      SqlConnection con = new SqlConnection("MyConnStringHere;"); 
      SqlCommand cmd = new SqlCommand("Select ID, CatType from PfCategoryType ORDER BY CatType", con); 
      con.Open(); 
      SqlDataReader rdr = cmd.ExecuteReader(); 

      while (rdr.Read()) 
      { 
       string CatType = (string)rdr["CatType"]; 
       _cattypes.Add(CatType); 

      } 

      con.Close(); 

      return _cattypes; 
     } 
    } 

在MainWindow.xaml.cs我:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     DataSource dataSource = new DataSource(); 
     this.DataContext = dataSource; 
    } 
} 

回答

0

如果檢查在VS中調試輸出你會看到實際的綁定錯誤。下面的代碼很可能會爲你修復它。

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=CatTypes}" /> 

如果您無法使RelativeSource正常工作,請使用名稱。屬性CatTypes是某個類的一個屬性,您爲某個控件創建了一個對象並將其設置爲datacontext。只要給控制一個名稱(例如myControl)和綁定是這樣的:

<ComboBox ItemsSource="{Binding ElementName=myControl, Path=CatTypes}" /> 

如果不工作,你需要發佈更多的代碼來找出你做錯了。

+0

沒有骰子。我沒有收到任何錯誤。只是一個空的組合框。 – Matt 2011-03-24 13:19:52

+0

我也使用WPF工具包中的數據網格,不確定這是否有所作爲。 – Matt 2011-03-24 13:22:26

+0

MainWindow的DataContext將DataContext設置爲DataSource類。我添加了額外的代碼。正如你所說的,我試圖給MainWindow對象添加一個名字,但是這仍然不起作用。也許額外的代碼會有所幫助。 – Matt 2011-03-24 13:59:51

0

如果您嘗試此操作,會發生什麼情況?

<ComboBox DataContext="{Binding DataContext, ElementName=myControl}" ItemsSource="{Binding CatTypes}" /> 

(當然你會重命名顯示「myControl」來匹配你的窗口的名稱。)

這裏,我們設置了組合框的數據上下文是相同的數據窗口的上下文。由於這也是您的XAML中第一個組合框的相同數據上下文,我想象第二個組合框將開始表現得像第一個組合框。 (雖然我擔心這會造成一些不必要的數據庫連接,每個網格行之一。)

編輯:

退一步講,如果你需要在該行的情況下設置其他的屬性,你贏了不想設置整個ComboBox的數據上下文。在那種情況下,我會嘗試這樣的事情。

<ComboBox ItemsSource="{Binding ElementName=myControl, Path=DataContext.CatTypes}" SelectedItem="{Binding CategoryType}" />