我已经定义了以下XAML。绑定填充了DataGridComboBoxColumn,如果我同时在组合框中选择PartNumber,则需要在DataGridTextColumn中绑定另一个属性(PartName)值。我怎么能在DataGridTextColumn中进行绑定?
<Window.Resources>
<ObjectDataProvider x:Key="RecordValues"
ObjectType="{x:Type local:RecordTemp}"
MethodName="GetPersonList">
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Grid>
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding}"
Margin="10"
Name="dataGrid1">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Combo"
Width="300"
SelectedItemBinding="{Binding Values}"
DisplayMemberPath="PartNumber"
ItemsSource="{Binding Source={StaticResource RecordValues}}" />
<DataGridTextColumn Header="Order Name"
Binding="" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
在C#中定义下面的代码,
ObservableCollection<RecordTemp> RecordsTemp = new ObservableCollection<RecordTemp>();
RecordsTemp.Add(new RecordTemp());
RecordsTemp.Add(new RecordTemp());
dataGrid1.DataContext = RecordsTemp;
public class RecordTemp
{
List<PartsList> _value = new List<PartsList>();
public RecordTemp()
{
_value.Add(new PartsList() { PartName = "One", PartNumber = "1", PartQuantity = 20 });
_value.Add(new PartsList() { PartName = "Two", PartNumber = "2", PartQuantity = 10 });
}
public List<PartsList> value
{
get { return _value; }
set { _value = value; }
}
public List<PartsList> GetPersonList()
{
return _value;
}
}
public class PartsList
{
public string PartName { get; set; }
public double PartQuantity { get; set; }
public string PartNumber { get; set; }
}
答案 0 :(得分:1)
实现这一目标的一种方法是
在RecordTemp类中添加一个新属性(比如属性名称为SelectedPart),该类将存储选定的PartList对象。
将SelectedPart属性绑定到ComboBox的SelectedValue属性。
将SelectedPart绑定到文本框。
现在,RecordTemp类应该实现INotifyPropertyChanged接口,以便在用户更改ComboBox值时更新UI。
以下是我对您的代码所做的修改。
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding}"
Margin="10"
Name="dataGrid1"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Combo">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding SelectedPart, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="PartNumber"
ItemsSource="{Binding Source={StaticResource RecordValues}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Order Name"
Binding="{Binding SelectedPart.PartName}" />
</DataGrid.Columns>
</DataGrid>
代码隐藏文件。您可能需要更改命名空间。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<RecordTemp> RecordsTemp = new ObservableCollection<RecordTemp>();
RecordsTemp.Add(new RecordTemp());
RecordsTemp.Add(new RecordTemp());
dataGrid1.DataContext = RecordsTemp;
}
}
public class RecordTemp : INotifyPropertyChanged
{
List<PartsList> _value = new List<PartsList>();
public RecordTemp()
{
_value.Add(new PartsList() { PartName = "One", PartNumber = "1", PartQuantity = 20 });
_value.Add(new PartsList() { PartName = "Two", PartNumber = "2", PartQuantity = 10 });
}
public List<PartsList> value
{
get { return _value; }
set { _value = value; }
}
private PartsList _SelectedPart;
public PartsList SelectedPart
{
get { return _SelectedPart; }
set
{
_SelectedPart = value;
OnPropertyChanged("SelectedPart");
}
}
public List<PartsList> GetPersonList()
{
return _value;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class PartsList
{
public string PartName { get; set; }
public double PartQuantity { get; set; }
public string PartNumber { get; set; }
}
}