将具有组合框列的DataGrid绑定到列表
本文关键字:DataGrid 绑定 列表 组合 | 更新日期: 2023-09-27 18:22:05
我有一个字典,它的值被用作DataGrid中comboboxColumn的源。任务如下:当用户从组合框中选择合适的项目时,应该将键而不是值写入列表。我的代码:
Dictionary<double, string> scores = new Dictionary<double, string>();
scores.Add(1, "the same");
scores.Add(3, "moderate superiority");
scores.Add(5, "strong superiority");
scores.Add(7, "very strong superiority");
scores.Add(9, "extremely superiority");
//define number of alternatives
int num = Alternatives.Children.Count - 1;
//initialize matrix for assessment scores
for (int i = 0; i < num; i++)
{
gridAssessment.Add(new double[num]);
}
//set initial values
for (int i = 0; i < num; i++)
{
gridAssessment[i][i] = scores.ElementAt(0).Key;
}
//define source for assessment grid
grAssessment.ItemsSource = gridAssessment;
grAssessment.AutoGenerateColumns = false;
//add columns to the grid
for (int i = 0; i < num; i++)
{
DataGridComboBoxColumn col = new DataGridComboBoxColumn();
grAssessment.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
//define source for comboboxes
col.ItemsSource = scores;
col.DisplayMemberPath = "Value";
col.SelectedValuePath = "Key";
string a = "[" + i.ToString() + "]";
Binding t = new Binding(a);
t.Mode = BindingMode.TwoWay;
t.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
col.SelectedValueBinding = t;
}
你能帮我装订吗?因为所选值的键没有存储在列表中:(
我使用了相同的代码,并尝试重新创建UI
背后的代码
列表<double[]>gridAssessment=新列表<double[]>();
public MainWindow()
{
InitializeComponent();
Dictionary<double, string> scores = new Dictionary<double, string>();
scores.Add(1, "the same");
scores.Add(3, "moderate superiority");
scores.Add(5, "strong superiority");
scores.Add(7, "very strong superiority");
scores.Add(9, "extremely superiority");
//define number of alternatives
int num = 3;//Alternatives.Children.Count - 1;
//initialize matrix for assessment scores
for (int i = 0; i < num; i++)
{
gridAssessment.Add(new double[num]);
}
//set initial values
for (int i = 0; i < num; i++)
{
gridAssessment[i][i] = scores.ElementAt(0).Key;
}
//define source for assessment grid
grAssessment.ItemsSource = gridAssessment;
grAssessment.AutoGenerateColumns = false;
//add columns to the grid
for (int i = 0; i < num; i++)
{
DataGridComboBoxColumn col = new DataGridComboBoxColumn();
grAssessment.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
//define source for comboboxes
col.ItemsSource = scores;
col.DisplayMemberPath = "Value";
col.SelectedValuePath = "Key";
string a = "[" + i.ToString() + "]";
Binding t = new Binding(a);
t.Mode = BindingMode.TwoWay;
t.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
col.SelectedValueBinding = t;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
StringBuilder str1 = new StringBuilder();
foreach (var c in gridAssessment)
{
str1.Append("<");
foreach (var item in c)
{
str1.AppendFormat(" {0} ", item);
}
str1.Append(">");
}
val.Content = str1.ToString();
}
XAML
<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<DataGrid Name="grAssessment" />
<Button Content="Check" Grid.Row="1" Click="Button_Click" />
<Label Name="val" Grid.Row="2" />
</Grid>
我看到显示的值为1,2,3。如果需要设置字符串值,则可能需要更改List<double[]>gridAssessment=新列表<double[]>()作为列表<string[]>gridAssessment=新列表<string[]>();并使用SelectedValuePath反转组合的DisplayMemberPath。
希望这能有所帮助!
我将Dictionary更改为ObservableCollection,现在它可以工作了。