【发布时间】:2017-04-27 06:04:52
【问题描述】:
我有一个数据网格
<DataGrid Name="dtgFeatures" Height="100" Margin="10" ColumnWidth="*" CanUserAddRows="True" MouseLeftButtonUp="DtgFeatures_MouseLeftButtonUp"/>
绑定到一个可观察的集合
ObservableCollection<CfgPartPrograms> obcCfgPartPrograms = new ObservableCollection<CfgPartPrograms>();
与
[Serializable]
public class CfgPartPrograms
{
public CfgPartPrograms() { }
public string Group{ get; set;}
public string Description{ get; set;}
public string Filename{ get; set;}<------set with openfiledialog
public string Notes{ get; set;}
}
现在,因为我希望能够使用 openfileDialog 插入文件名,所以我添加了以下代码:
private void DtgFeatures_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int column = (sender as DataGrid).CurrentCell.Column.DisplayIndex;
if ( column == 2)
{
OpenFileDialog ofdPP = new OpenFileDialog();
if (ofdPP.ShowDialog() == true)
{
if (obcCfgPartPrograms.Count == 0)
obcCfgPartPrograms.Add(new CfgPartPrograms() { Filename = ofdPP.FileName });
else
obcCfgPartPrograms[selectedIndex].Filename = ofdPP.FileName;
dtgFeatures.ItemsSource = null;
dtgFeatures.ItemsSource = obcCfgPartPrograms;
}
}
问题是,当我设置文件名时,可观察集合尚未更新。 我会用图片来解释:
所以我现在添加了 aaaa 和 bbb,我想使用上面的代码强制文件名,但是当我这样做时,尚未对 observable 集合执行绑定操作,因此 aaaa 和 bbbb 不存在。
简而言之如何告诉绑定的数据网格更新绑定??
提前致谢 帕特里克
【问题讨论】:
标签: c# wpf data-binding datagrid observablecollection