简体   繁体   English

C#/ WPF:将Datagrid中的Combobox ItemSource绑定到DataContext外部的元素

[英]C#/WPF: Binding Combobox ItemSource in Datagrid to element outside of the DataContext

I'd like to do following: 我想做以下事情:

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;
}

Now my (WPF Toolkit) DataGrid looks like this: 现在我的(WPF Toolkit)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>

Now my problem is, that PreLoadedUserList is outside of the ItemSource (SomeDataRowList) and I don't know how to bind to something outside of it. 现在我的问题是,PreLoadedUserList在ItemSource(SomeDataRowList)之外,我不知道如何绑定到它之外的东西。 What I actually want it: - Display in the ComboBox PreLoadedUserList - Set the Value of (RowEntries) SelectedItem.UserAge to the Value of the selected ComboboxItem.Age 我真正想要它: - 在ComboBox中显示PreLoadedUserList - 将(RowEntries)SelectedItem.UserAge的值设置为所选ComboboxItem.Age的值

Let me know if my explanation is too weird :-) 如果我的解释太奇怪,请告诉我:-)

Thank you, Cheers 谢谢,干杯

Here we go :-) 开始了 :-)

<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>

Hope this can help someone else too. 希望这也可以帮助其他人。

Cheers 干杯

If RowEntries is a custom class, just give it a reference to the PreLoadedUserList. 如果RowEntries是自定义类,只需为它提供对PreLoadedUserList的引用。 Then, each instance has a pointer to it and you can use it in your binding. 然后,每个实例都有一个指向它的指针,您可以在绑定中使用它。

Just a suggestion, class names like Users and RowEntries suggest that they are collections but your usage looks like they're the item not the collection. 只是一个建议,像Users和RowEntries这样的类名称表明它们是集合,但你的用法看起来就像是项目而不是集合。 I'd use singular names to avoid any confusion. 我会使用单数名称来避免混淆。 I'd do something like this 我会做这样的事情

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM