WPF MVVM DataGrid行到另一个DataGrid

在我的WPF应用程序使用MVVM,我有一个DataGrid(dgSelectCase)从我的ViewModel充满了绑定ObservableCollection。一个按钮出现在第一列中,单击时,应该将选定的行添加到第二个DataGrid(dgCaseSelected)。WPF MVVM DataGrid行到另一个DataGrid

View DataContext绑定到ViewModel后面的代码中,我知道它可以工作,因为页面上的其他控件(组合框,文本框等)完美地工作。 dgSelectCase中的“添加”按钮命令绑定到ViewModel中的AddTo方法,并且dgCaseSelected中的“Remove”按钮命令绑定到ViewModel中的RemoveFrom方法。

“添加”按钮不起作用,但更重要的是,它看起来像我选择了dgSelectCase DataGrid中的绑定问题,因为当我点击一行时,DataGrid周围出现一个红框。我哪里错了?感谢任何和所有的帮助,因为我还在学习......慢慢:)。以下是代码片段。

XAML

 <DataGrid> 

<DataGrid.Columns>

<DataGridTemplateColumn Header="Select">

<DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<Button Content="Add" Command="{Binding AddTo}"/>

</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

<DataGridTextColumn Header="Fac ID #" Binding="{Binding FacilityIDNum}"/>

<DataGridTextColumn Header="OP Case #" Binding="{Binding CaseBookingNum}"/>

<DataGridTextColumn Header="Booking Type #" Binding="{Binding BookingTypeNum}"/>

<DataGridTextColumn Header="Booking Type" Binding="{Binding BookingType}"/>

</DataGrid.Columns>

</DataGrid>

<DataGrid x:Name="dgCaseSelected"

AutoGenerateColumns="False"

ItemsSource="{Binding DGCaseBookingsSelected}"

>

<DataGrid.Columns>

<DataGridTemplateColumn Header="Select">

<DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<Button Content="Remove" Command="{Binding RemoveFrom}"/>

</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

<DataGridTextColumn Header="Fac ID #" Binding="{Binding FacilityIDNum}"/>

<DataGridTextColumn Header="OP Case #" Binding="{Binding CaseBookingNum}"/>

<DataGridTextColumn Header="Booking Type #" Binding="{Binding BookingTypeNum}"/>

<DataGridTextColumn Header="Booking Type" Binding="{Binding BookingType}"/>

</DataGrid.Columns>

</DataGrid>

视图模型

private ObservableCollection<DGCaseBookings> _dgCaseBookingsList; 

private ObservableCollection<DGCaseBookings> _dgSelectedCaseBookings;

private ObservableCollection<DGCaseBookings> _dgCaseBookingsSelected;

public ObservableCollection<DGCaseBookings> DGCaseBookingsList

{

get { return _dgCaseBookingsList; }

set

{

SetProperty(ref _dgCaseBookingsList, value,() => DGCaseBookingsList);

}

}

public ObservableCollection<DGCaseBookings> DGSelectedCaseBookings

{

get { return _dgSelectedCaseBookings; }

set

{

SetProperty(ref _dgSelectedCaseBookings, value,() => DGSelectedCaseBookings);

}

}

public ObservableCollection<DGCaseBookings> DGCaseBookingsSelected

{

get { return _dgCaseBookingsSelected; }

set

{

SetProperty(ref _dgCaseBookingsSelected, value,() => DGCaseBookingsSelected);

}

}

public CMBookingSelectVM() : base()

{

DGCaseBookingsList = new ObservableCollection<DGCaseBookings>();

DGCaseBookingsSelected = new ObservableCollection<DGCaseBookings>();

}

private void fillDGCaseBookingswithFacility()

{

using (MySqlConnection con = new MySqlConnection(dbConnectionString))

{

DGCaseBookingsList = new ObservableCollection<DGCaseBookings>();

con.Open();

string Query = ///SELECT STATEMENT WORKS FINE///

MySqlCommand createCommand = new MySqlCommand(Query, con);

MySqlDataReader dr = createCommand.ExecuteReader();

int count = 1;

while (dr.Read())

{

int FacilityIDNum = dr.GetInt32(0);

int CaseBookingNum = dr.GetInt32(1);

int BookingTypeNum = dr.GetInt32(2);

string BookingType = dr.GetString(3);

DGCaseBookings dgcb = new DGCaseBookings(count, FacilityIDNum, CaseBookingNum, BookingTypeNum, BookingType);

DGCaseBookingsList.Add(dgcb);

count++;

}

con.Close();

}

}

public void AddTo(DGCaseBookings dgcb)

{

if (dgcb != null)

{

DGCaseBookingsSelected.Add(dgcb);

DGCaseBookingsList.Remove(dgcb);

}

}

public void RemoveFrom(DGCaseBookings dgcb)

{

if (dgcb != null)

{

DGCaseBookingsList.Add(dgcb);

DGCaseBookingsSelected.Remove(dgcb);

}

}

回答:

首先,你没有在你的代码中提到你是如何想的DataGrid的选择项属性绑定到你的集合(事实上,你实际上甚至没有设置第一个datagrid的itemsource),我想你没有这样做,因为你实际上不能绑定datagrid的210属性,因为它是只读属性。从现在起,你只有2个选择。找到解决方法将选定的项目发送到您的视图模型,或在单个行上工作。至于第二个方法,您可以简单地这样做:

<DataGrid ItemsSource="{Binding DGCaseBookingsList}" SelectedItem="{Binding MyItem}" SelectionMode = "Single" SelectionUnit="FullRow"> 

<DataGrid.Columns>

<DataGridTemplateColumn Header="Select">

<DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<Button Content="Add" Command="{Binding AddTo}"/>

</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

<DataGridTextColumn Header="Fac ID #" Binding="{Binding FacilityIDNum}"/>

<DataGridTextColumn Header="OP Case #" Binding="{Binding CaseBookingNum}"/>

<DataGridTextColumn Header="Booking Type #" Binding="{Binding BookingTypeNum}"/>

<DataGridTextColumn Header="Booking Type" Binding="{Binding BookingType}"/>

</DataGrid.Columns>

</DataGrid>

在你的虚拟机删除DGSelectedCaseBookings

,并添加以下内容:

private DGCaseBookings myItem = new DGCaseBookings(); 

public DGCaseBookings MyItem

{

get { return myItem; }

set

{

SetProperty(ref myItem, value,() => MyItem);

}

}

从那里,你将处理使用按钮的命令这个对象。只要确保实施INotifyPropertyChanged,或者在用户界面中不会看到任何更改。

如果您的目标是多选,那么最简单的方法就是处理数据网格的选择更改事件。从那里你可以做这样的事情。

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 

{

MyVM vm = this.DataContext as MyVM;

DataGrid s = sender as DataGrid;

vm.DGSelectedCaseBookings = s.SelectedItems;

}

回答:

在这个背景下,你可以设置事件,当你点击按钮并添加当前行(如事件的参数)。取当前行从数据源/ ObservableCollection中选择它并将其添加到另一个数据集中,例如_dgSelectedCaseBookings。

之后,您可以刷新UI上新的_dgSelectedCaseBookings。

以上是 WPF MVVM DataGrid行到另一个DataGrid 的全部内容, 来源链接: utcz.com/qa/258436.html

回到顶部