用于从主窗口在用户控件中添加和删除的绑定命令,以在两个不同的DataGrid中添加和删除行

时间:2019-04-03 14:44:49

标签: wpf user-controls commandbinding

我正在研究一个XAML项目,该项目具有一个带有两个按钮(添加和删除)的用户控件,用于两个不同的DataGrid。

我想要的是什么? 将用户控件与数据网格链接,以从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}"/>

.window.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);
        }

我想要的是只有两个按钮和两个命令,这两个按钮对于两个用于添加和删除行的数据网格都应该起作用。

有人可以帮我吗?

0 个答案:

没有答案