gpt4 book ai didi

wpf - 当 DataGrid 绑定(bind)到 DataTable 时,如何使用 MVVM 在 WPF 中选择 DataGrid 行

转载 作者:行者123 更新时间:2023-12-03 11:00:03 29 4
gpt4 key购买 nike

我已经成功绑定(bind)了DataTableDataGrid控制在 WPF with MVVM . (我在 viewmodel 中定义了 DataTable。)

然后我定义了一个 DataRowView type 属性并绑定(bind)到 SelectedItem DataGrid 控件的属性。

我可以通过它获得选定的项目。但我试图设置选定的项目,但我找不到办法。有人可以帮我弄清楚。
view

<Window x:Class="Pivot.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Pivot.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Grid>

<Grid.DataContext>
<vm:MainViewModel />
</Grid.DataContext>

<DataGrid
ItemsSource="{Binding SizeQuantityTable}"
AutoGenerateColumns="True"
SelectedValue="{Binding SelectedRow}"
Margin="0,0,0,120" />
</Grid>
</Window>
View Model
public class MainViewModel : ViewModelBase
{
#region Declarations

private DataTable sizeQuantityTable;
private DataRowView selectedRow;

#endregion

#region Properties

/// <summary>
/// Gets or sets the size quantity table.
/// </summary>
/// <value>The size quantity table.</value>
public DataTable SizeQuantityTable
{
get
{
return sizeQuantityTable;
}
set
{
sizeQuantityTable = value;
NotifyPropertyChanged("SizeQuantityTable");
}
}

/// <summary>
/// Gets or sets the selected row.
/// </summary>
/// <value>The selected row.</value>
public DataRowView SelectedRow
{
get
{
return selectedRow;
}
set
{
selectedRow = value;
NotifyPropertyChanged("SelectedRow");
}
}

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="MainViewModel"/> class.
/// </summary>
public MainViewModel()
{
this.SizeQuantityTable = new DataTable();

DataColumn sizeQuantityColumn = new DataColumn();
sizeQuantityColumn.ColumnName = "Size Quantity";
this.SizeQuantityTable.Columns.Add(sizeQuantityColumn);

DataColumn sColumn = new DataColumn();
sColumn.ColumnName = "S";
this.SizeQuantityTable.Columns.Add(sColumn);

DataColumn mColumn = new DataColumn();
mColumn.ColumnName = "M";
this.SizeQuantityTable.Columns.Add(mColumn);

DataRow row1 = this.SizeQuantityTable.NewRow();
row1[sizeQuantityColumn] = "Blue";
row1[sColumn] = "12";
row1[mColumn] = "15";
this.SizeQuantityTable.Rows.Add(row1);

DataRow row2 = this.SizeQuantityTable.NewRow();
row2[sizeQuantityColumn] = "Red";
row2[sColumn] = "18";
row2[mColumn] = "21";
this.SizeQuantityTable.Rows.Add(row2);

DataRow row3 = this.SizeQuantityTable.NewRow();
row3[sizeQuantityColumn] = "Green";
row3[sColumn] = "24";
row3[mColumn] = "27";
this.SizeQuantityTable.Rows.Add(row3);

DataRow row4 = this.SizeQuantityTable.NewRow();
row4[sizeQuantityColumn] = "Yellow";
row4[sColumn] = "30";
row4[mColumn] = "33";
this.SizeQuantityTable.Rows.Add(row4);

}

#endregion
}

最佳答案

如果我理解正确,您想从 MainViewModel 类中以编程方式选择网格行。如果这是需要,请尝试以下代码。 SelectRow 是中的方法
主视图模型。这可能不是完整的解决方案,但对您的要求有一些想法。

public void SelectRow(int rowIndex)
{
SelectedRow = SizeQuantityTable.DefaultView[rowIndex];
}

关于wpf - 当 DataGrid 绑定(bind)到 DataTable 时,如何使用 MVVM 在 WPF 中选择 DataGrid 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15782827/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com