当 DataGrid 绑定到 DataTable 时,如何使用 MVVM 在 WPF 中选择 DataGrid 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15782827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a DataGrid row in WPF with MVVM when the DataGrid is bound to a DataTable
提问by Haritha
I have successfully bound a DataTableto a DataGridcontrol in WPF with MVVM. (I have defined the DataTable in the viewmodel.)
我已经成功绑定一个DataTable到一个DataGrid控制WPF with MVVM。(我已经在viewmodel. 中定义了 DataTable 。)
Then I have defined a DataRowViewtype property and bound to the SelectedItemproperty of the DataGrid control.
然后我定义了一个DataRowView类型属性并绑定到SelectedItemDataGrid 控件的属性。
I can get the selected item through that. But I tried to set the selected item but I couldn't find a way to do it. Can somebody help me to figure it out.
我可以通过它获得所选项目。但是我尝试设置所选项目,但找不到方法。有人可以帮我弄清楚。
The view
这 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>
The View Model
这 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
}
回答by RonakThakkar
If i understand correctly, you want to select grid row programatically from MainViewModel class. If that is the need then try below code. SelectRow is the method in MainViewModel. This might not be complete solution but some idea for you requirement.
如果我理解正确,您想从 MainViewModel 类中以编程方式选择网格行。如果这是需要,请尝试以下代码。SelectRow 是 MainViewModel 中的方法。这可能不是完整的解决方案,而是针对您的需求的一些想法。
public void SelectRow(int rowIndex)
{
SelectedRow = SizeQuantityTable.DefaultView[rowIndex];
}
回答by digitally_inspired
Long pending question??. I made this modification in XAML and with it on button click I am able to change grid selection based on textbox row value.
长期悬而未决的问题??。我在 XAML 中进行了此修改,并通过单击按钮单击我能够根据文本框行值更改网格选择。
<Grid>
<Grid.DataContext>
<vm:MainViewModel x:Name="Model"/>
</Grid.DataContext>
<DataGrid
ItemsSource="{Binding SizeQuantityTable}"
AutoGenerateColumns="True"
SelectedIndex="{Binding SelectedRow, Mode=TwoWay}"
Margin="0,0,0,120" />
<Button Content="Button" Height="53" HorizontalAlignment="Left" Margin="121,214,0,0" Name="button1" VerticalAlignment="Top" Width="118" Click="button1_Click" />
<TextBox Height="21" HorizontalAlignment="Left" Margin="272,218,0,0" Name="textBox1" VerticalAlignment="Top" Width="114" Text="1" />
</Grid>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//I know this is not the correct way to do it in MVVM but I am am taking time from my work... so need to be quick.. :)
private void button1_Click(object sender, RoutedEventArgs e)
{
Model.SelectedRow = int.Parse(textBox1.Text);
}
}
private int selectedRow;
/// <summary>
/// Gets or sets the selected row.
/// </summary>
/// <value>The selected row.</value>
public int SelectedRow
{
get
{
return selectedRow;
}
set
{
selectedRow = value;
OnPropertyChanged("SelectedRow");
}
}
Try this. It should work...
尝试这个。它应该工作...

