2011-03-24 114 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}" />