Я работаю над проектом XAML, в котором имеется один пользовательский элемент управления с двумя кнопками (Добавить и удалить) для двух разных массивов данных.
Что я хочу?
Связать пользовательский элемент управления с сетками данных для добавления и удаления строк из DataGrid.
Я попробовал привязку команд, используя свойства зависимости, и она работает, но я использовал 4 разные команды вместо двух отдельных команд.
Это пользовательский элемент управления
<UserControl x:Class="Program3_2.GridCommandStrip"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Program3_2"
mc:Ignorable="d">
<WrapPanel Orientation="Horizontal">
<Button x:Name="btnAdd"
Content="Add"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GridCommandStrip}}, Path=AddButtonValue}"/>
<Separator Style="{StaticResource SepW05VH}"/>
<Button x:Name="btnDelete"
Content="Delete"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GridCommandStrip}}, Path=DeleteButtonValue}"/>
</WrapPanel>
</UserControl>
.cs Файл
using System.Windows.Controls;
using System.Windows.Input;
namespace Program3_2
{
/// <summary>
/// Interaction logic for GridCommandStrip.xaml
/// </summary>
public partial class GridCommandStrip : UserControl
{
public static readonly DependencyProperty AddCommandProperty = DependencyProperty.Register("AddButtonValue", typeof(ICommand), typeof(GridCommandStrip));
public ICommand AddButtonValue
{
get { return (ICommand)GetValue(AddCommandProperty); }
set { SetValue(AddCommandProperty, value); }
}
public static readonly DependencyProperty DeleteCommandProperty = DependencyProperty.Register("DeleteButtonValue", typeof(ICommand), typeof(GridCommandStrip));
public ICommand DeleteButtonValue
{
get { return (ICommand)GetValue(DeleteCommandProperty); }
set { SetValue(DeleteCommandProperty, value); }
}
public GridCommandStrip()
{
InitializeComponent();
}
}
}
Используется пользовательский Conrtol, как показано ниже
<local:GridCommandStrip x:Name="ucGridDepartement"
Style="{StaticResource usercontolstyle}"
AddButtonValue="{x:Static local:MainWindow.addButtonGridDepartementCommand}"
DeleteButtonValue="{x:Static local:MainWindow.deleteButtonGridDepartementCommand}"/>
<local:GridCommandStrip x:Name="ucGridStaff"
Style="{StaticResource usercontolstyle}"
AddButtonValue="{x:Static local:MainWindow.addButtonGridStaffCommand}"
DeleteButtonValue="{x:Static local:MainWindow.deleteButtonGridStaffCommand}"/>
.cs файл главного окна
public partial class MainWindow : Window
{
#region Data Member Declaration
Departement DepartementDataItem;
Staff StaffDataItem;
ObservableCollection<Departement> DepartementData;
ObservableCollection<Staff> StaffData;
public static RoutedCommand addButtonGridStaffCommand = new RoutedCommand();
public static RoutedCommand deleteButtonGridStaffCommand = new RoutedCommand();
public static RoutedCommand addButtonGridDepartementCommand = new RoutedCommand();
public static RoutedCommand deleteButtonGridDepartementCommand = new RoutedCommand();
#endregion
#region Data Methods Definiton
/// <summary>
/// Initializes Data For both the grids and other elements
/// </summary>
public MainWindow()
{
InitializeComponent();
DepartementData = new ObservableCollection<Departement>(); ////To Store all the Departement Data
DepartementData.Add(new Departement("O1", "DN1", "HOD1"));
dataGridDepartement.ItemsSource = DepartementData; ////Initialized Departement Data to DataGrid
StaffData = new ObservableCollection<Staff>(); ////To Store all the Staff Data
StaffData.Add(new Staff("FN1", "MN1", "LN1", Convert.ToDateTime("2016-03-02"), "Male", "5ft 6")); ////Date with user defined(given) date
StaffData.Add(new Staff("FN2", "MN2", "LN2", DateTime.Now, "Female", "5ft 6")); ////Date with Today Date
dataGridStaff.ItemsSource = StaffData; ////Initialized Staff Data to DataGrid
CommandBinding AddDepartement= new CommandBinding(addButtonGridDepartementCommand,ExecuteAddButtonGridDepartementCommand);
CommandBinding DeleteDepartement = new CommandBinding(deleteButtonGridDepartementCommand,ExecuteDeleteButtonGridDepartementCommand);
CommandBinding AddStaff = new CommandBinding(addButtonGridStaffCommand,ExecuteAddButtonGridStaffCommand);
CommandBinding DeleteStaff = new CommandBinding(deleteButtonGridStaffCommand,ExecuteDeleteButtonGridStaffCommand);
this.CommandBindings.Add(AddDepartement);
this.CommandBindings.Add(DeleteDepartement);
this.CommandBindings.Add(AddStaff);
this.CommandBindings.Add(DeleteStaff);
cmbDepartementID.ItemsSource = DepartementData; ////Search Combobox Binding with Departement ID
}
/// <summary>
/// Add New Data to departement
/// </summary>
public void ExecuteAddButtonGridDepartementCommand(object sender, ExecutedRoutedEventArgs e)
{
DepartementDataItem = new Departement();
DepartementData.Add(DepartementDataItem);
dataGridDepartement.SelectedItem = DepartementDataItem;
dataGridDepartement.ScrollIntoView(dataGridDepartement.SelectedItem);
}
/// <summary>
/// Delete Data From Departement
/// </summary>
public void ExecuteDeleteButtonGridDepartementCommand(object sender, ExecutedRoutedEventArgs e)
{
DepartementDataItem = (Departement)dataGridDepartement.SelectedItem;
DepartementData.Remove(DepartementDataItem);
}
/// <summary>
/// Add New Data to Staff
/// </summary>
public void ExecuteAddButtonGridStaffCommand(object sender, ExecutedRoutedEventArgs e)
{
StaffDataItem = new Staff();
StaffData.Add(StaffDataItem);
dataGridStaff.SelectedItem = StaffDataItem;
dataGridStaff.ScrollIntoView(dataGridStaff.SelectedItem);
}
/// <summary>
/// Delete Data From Staff
/// </summary>
public void ExecuteDeleteButtonGridStaffCommand(object sender, ExecutedRoutedEventArgs e)
{
StaffDataItem = (Staff)dataGridStaff.SelectedItem;
StaffData.Remove(StaffDataItem);
}
Мне нужны только две кнопки с двумя командами, которые должны работать как с DataGrids для добавления, так и удаления строк.
Может кто-нибудь помочь мне с этим?