我将使用带有两个带标签的文本框的简单对话,新对将添加到原始数据中,应绑定到原始数据,DataGrid以便自动生成行.
编辑:DataGrid示例解决方案.
XAML:
<Window
...
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<StackPanel Orientation="Vertical">
<DataGrid ItemsSource="{Binding GridData}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
在窗口类中:
private ObservableCollection<Pair> gridData = new ObservableCollection<Pair>(new Pair[]{new Pair()});
public ObservableCollection<Pair> GridData
{
get { return gridData; }
}
Run Code Online (Sandbox Code Playgroud)
配对类:
public class Pair : INotifyPropertyChanged
{
private string key = "Key";
public string Key
{
get { return key; }
set
{
if (this.key != value)
{
key = value;
NotifyPropertyChanged("Key");
}
}
}
private double value = 0;
public double Value
{
get { return value; }
set
{
if (this.value != value)
{
this.value = value;
NotifyPropertyChanged("Value");
}
}
}
public Pair() { }
public Pair(string key, double value)
: this()
{
Key = key;
Value = value;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
您应该能够添加具有正常DataGrid功能的新对.
| 归档时间: |
|
| 查看次数: |
4449 次 |
| 最近记录: |