我想将一个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