【问题标题】:C#/WPF: Binding Combobox ItemSource in Datagrid to element outside of the DataContextC#/WPF:将 Datagrid 中的 Combobox ItemSsource 绑定到 DataContext 之外的元素
【发布时间】:2010-12-02 14:21:24
【问题描述】:

我想做以下事情:

public List<Users> PreLoadedUserList { get; set; }
public List<RowEntries> SomeDataRowList { get; set; }

public class Users
{
    public int Age { get; set; }
    public string Name { get; set; }
}
public class SomeDataRowList 
{
    public int UserAge { get; set;
}

现在我的(WPF 工具包)DataGrid 看起来像这样:

<my:DataGrid AutoGenerateColumns="False" MinHeight="200" 
             ItemsSource="{Binding Path=SomeDataRowList}">
    <my:DataGridComboBoxColumn Header="Age" 
                               ItemsSource="{Binding Path=PreLoadedUserList}" 
                               DisplayMemberPath="Name" 
                               SelectedValueBinding="{Binding Path=UserAge}"/>

</my:DataGrid>

现在我的问题是,PreLoadedUserList 在 ItemSource (SomeDataRowList) 之外,我不知道如何绑定到它之外的东西。我真正想要的: - 在 ComboBox PreLoadedUserList 中显示 - 将 (RowEntries) SelectedItem.UserAge 的 Value 设置为所选 ComboboxItem.Age 的 Value

如果我的解释太奇怪了,请告诉我:-)

谢谢你, 干杯

【问题讨论】:

    标签: c# wpf datagrid binding combobox


    【解决方案1】:

    如果 RowEntries 是自定义类,只需为其提供对 PreLoadedUserList 的引用。然后,每个实例都有一个指向它的指针,您可以在绑定中使用它。

    只是一个建议,像 Users 和 RowEntries 这样的类名表明它们是集合,但您的使用看起来像是项目而不是集合。我会使用单数名称以避免任何混淆。我会做这样的事情

    public List<User> PreLoadedUserList { get; set; }
    public List<RowEntry> SomeDataRowList { get; set; }
    
    public class User
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }
    public class RowEntry 
    {
        public int UserAge { get; set; }
        public List<User> PreLoadedUserList { get; set; }
    }
    
    // at the point where both PreLoadedUserList is instantiated
    // and SomeDataRowList is populated
    SomeDataRowList.ForEach(row => row.PreLoadedUserList = PreLoadedUserList);
    

    【讨论】:

      【解决方案2】:

      我们开始:-)

      <my:DataGridTemplateColumn Header="SomeHeader">
          <my:DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <ComboBox SelectedValuePath="UserAge" 
                      SelectedValue="{Binding Age}" 
                      DisplayMemberPath="Name" 
                      ItemsSource="{Binding Path=DataContext.PreLoadedUserList, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                      IsReadOnly="True" Background="White" />
              </DataTemplate>
          </my:DataGridTemplateColumn.CellTemplate>
      </my:DataGridTemplateColumn>
      

      希望这对其他人也有帮助。

      干杯

      【讨论】:

      • 这对我帮助很大...谢谢!
      • 哇,我一直在尝试使用“DataGridComboBoxColumn”,但什么也没有……但是然后魔术!你漂亮的例子工作(与模板的东西)谢谢!
      • 这帮助我解决了我的问题,约瑟夫。处理这些标题组合框是我的下一个任务。您的解决方案帮助了我很多,节省了我很多时间。非常感谢。
      猜你喜欢
      • 2020-01-30
      • 1970-01-01
      • 2013-11-16
      • 2015-04-07
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2013-06-30
      • 2011-07-28
      相关资源
      最近更新 更多