2017-12-02 198 views
0

在我的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); 
     } 
    } 

回答

0

首先,你沒有在你的代碼中提到你是如何想的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; 
} 
0

在這個背景下,你可以設置事件,當你點擊按鈕並添加當前行(如事件的參數)。取當前行從數據源/ ObservableCollection中選擇它並將其添加到另一個數據集中,例如_dgSelectedCaseBookings。

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