C# 和 WPF - DataGridComboBoxColumn 上 ItemsSource 上的绑定列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14614894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# and WPF - Binding List on ItemsSource on DataGridComboBoxColumn
提问by werasquez
I want to bind List(which contains data from LINQ query) with DataGridComboBoxColumnas ItemsSource. SelectedValueproperty is taken from other Listwith data for whole DataGrid.
我想List用DataGridComboBoxColumnas绑定(其中包含来自 LINQ 查询的数据)ItemsSource。SelectedValue财产是从 other 中获取的,List并带有整个数据DataGrid。
XAML:
XAML:
<DataGrid x:Name="DG1" x:Uid="DG1" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,5,0,0" HorizontalAlignment="Stretch">
<DataGrid.Columns>
//...
<DataGridComboBoxColumn ItemsSource="{Binding MyValueItem}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>
C#:
C#:
public class ValuesInfo
{
public int id { get; set; }
public string number { get; set; }
public string myvalue { get; set; }
}
public class MyValueItems : List<string>
{
}
public partial class MyWindow : Window
{
public MyValueItems MyValueItem { get; set; }
public MyWindow()
{
InitializeComponent();
}
private void MyWindow_Load(object sender, RoutedEventArgs e)
{
MyValueItem = new MyValueItems();
sqlDataContext dc = new sqlDataContext();
var allValueNames = (from p in dc.Names
orderby p.id ascending
select p.name);
MyValueItem.AddRange(allValueNames);
DG1.DataContext = MyValueItem;
}
public void LoadValues()
{
List<ValuesInfo> ValueList = new List<ValuesInfo> { };
//...
for (int i = 1; i <= (int)rdr[0]; i++)
{
var dbset = (from p in dc.Values
where p.id == i
orderby p.id ascending
select p).Single();
var ValueName = (from p in dc.Names
where p.id == dbset.valueId
select p.name).Single();
ValuesInfo valueItem = new ValuesInfo
{
id = dbset.id,
number = dbset.number,
myvalue = valueName
};
ValueList.Add(valueItem);
}
DG1.ItemsSource = ValueList;
}
}
Here are a lot of examples of DataGridComboBoxColumnBinding but none of these can help to solve my issue.
这里有很多DataGridComboBoxColumnBinding的例子,但这些都不能帮助解决我的问题。
回答by Phillip Scott Givens
It looks like you are asking for a property on your DataContext called MyValueItem.
看起来您在 DataContext 上要求一个名为 MyValueItem 的属性。
<DataGridComboBoxColumn ItemsSource="{Binding MyValueItem}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>
But you are passing the value of MyValueItem as the DataContext.
但是您将 MyValueItem 的值作为 DataContext 传递。
DG1.DataContext = MyValueItem;
You need to set the DataContext to an item with a "MyValueItem" property. Replace the above line with the one below.
您需要将 DataContext 设置为具有“MyValueItem”属性的项目。将上面的一行替换为下面的一行。
DG1.DataContext = this;
Where "this" is the MyWindow which has the MyValueItem property.
其中“this”是具有 MyValueItem 属性的 MyWindow。
...
...
Alternatively, you could change the binding to the below, which will take whatever item you pass to it as the binding source.
或者,您可以将绑定更改为以下内容,这将采用您传递给它的任何项目作为绑定源。
<DataGridComboBoxColumn ItemsSource="{Binding}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>

