我是 WPF 编程的新手。我写了一个关于 WPF DataGrid 控件的简单示例并使用了 MVVM 模式。我想要做的是在 DataGrid 控件中显示数据。但是,数据总是在数据网格中重复。输出如下图:
代码片段:
// Model:
Public class Machine
{
Private string _uuid;
public string UUID {get{ return _uuid; } set{ _uuid = value; }}
public Targets(string uuid)
{
UUID = uuid;
}
}
// ViewModel:
public class MainWindowViewModel
{
private readonly ObservableCollection<Machine> _machine = new ObservableCollection<Machine>();
public MainWindowViewModel()
{
_machine.Add(new Machine("1234"));
}
public ObservableCollection< Machine > Machines
{
get { return _machine; }
}
}
// XAML:
<DataGrid Name="datagGrid1" Margin="2,2,2,2" ItemsSource="{Binding Machines}" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Width="120"
Binding="{Binding UUID}"
Header="UUID-S" />
</DataGrid.Columns>
</DataGrid>