本文介绍了如何绑定wpf DataGridComboBoxColumn ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataGridComboBoxColumn dataCombox = new DataGridComboBoxColumn();
dataCombox.Header ="Name";
Binding b = new Binding();
b.Path = new PropertyPath("ComboxClass");
dataCombox.ItemsSource = listCom;
dataCombox.SelectedItemBinding = b;
dataCombox.SelectedValuePath = "PriceTypeId";
dataCombox.DisplayMemberPath = "PriceTypeValue";
dgForData.Columns.Add(dataCombox);
然后绑定
and then Binding
ObservableCollection<CommClassForDataGrid> listData = new ObservableCollection<CommClassForDataGrid>();
string path = comSelect.comPath;
IEnumerable<string> fileNameList = new List<string>();
fileNameList = FileTools.GetLastDirXmlNodeString(path);
foreach (string filePath in fileNameList)
{
CommClassForDataGrid com = new CommClassForDataGrid();
CommClassForComBox cb = new CommClassForComBox();
cb.PriceTypeValue = "xiaoming";
cb.PriceTypeId = comSelect.flag;
listData.Add(com);
}
this.dgForData.ItemsSource = listData;
我希望得到一组默认的是xiaoming DataGridComboBoxColumn数据。
现在界面显示没有值的方式
I hope to get a set of default is xiaoming DataGridComboBoxColumn data.
Now the way the interface displays no value
推荐答案
Bind a List to the ItemsSource of the DataGridComboBoxColumn and then specify the SelectedItemBinding, DisplayMemberPath, SelectedValueBinding or the SelectedValuePath property.
Below is sample code from social.msdn site:
XAML:
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="308">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Position" SelectedItemBinding="{Binding Position}"/>
</DataGrid.Columns>
</DataGrid>
Code:
public partial class MainWindow : Window
{
public ObservableCollection<Player> Players { get; set; }
public ObservableCollection<string> Positions { get; set; }
public MainWindow()
{
Positions = new ObservableCollection<string>() { "Forward", "Defense", "Goalie" };
Players = new ObservableCollection<Player>(){
new Player() {Name = "Tom",Position= "Forward"},
new Player() {Name = "Dick", Position= "Defense"},
new Player() {Name = "Harry", Position= "Goalie"}
};
InitializeComponent();
ComboBoxColumn.ItemsSource = Positions;
dataGrid1.ItemsSource = Players;
}
}
public class Player
{
public string Name { set; get; }
public string Position { set; get; }
}
这篇关于如何绑定wpf DataGridComboBoxColumn ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!