我想將一個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;
}
}
沒有骰子。我沒有收到任何錯誤。只是一個空的組合框。 – Matt 2011-03-24 13:19:52
我也使用WPF工具包中的數據網格,不確定這是否有所作爲。 – Matt 2011-03-24 13:22:26
MainWindow的DataContext將DataContext設置爲DataSource類。我添加了額外的代碼。正如你所說的,我試圖給MainWindow對象添加一個名字,但是這仍然不起作用。也許額外的代碼會有所幫助。 – Matt 2011-03-24 13:59:51