C#/ WPF:绑定组合框的ItemSource DataGrid中以元素DataContext的外 [英] C#/WPF: Binding Combobox ItemSource in Datagrid to element outside of the DataContext
问题描述
我想做如下:
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是这样的:
Now my (WPF Toolkit) DataGrid looks like this:
<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的值设置为所选ComboboxItem.Age的价值
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. 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
让我知道,如果我的解释是太诡异了: - )
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.
干杯
这篇关于C#/ WPF:绑定组合框的ItemSource DataGrid中以元素DataContext的外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!