簡體   English   中英

WPF按鈕按鈕上的新DataGrid行參數NULL

[英]WPF New DataGrid Row Parameter NULL on Button Command

WPF的新功能。 我正在構建的第一個應用程序是使用帶有CommandCommand的RelayCommand和DataGrid編輯或輸入新用戶。 對於具有在打開的行中輸入值的新用戶,按下時將調用OneditButtonCommand,但作為參數傳遞的Item始終為null。 我嘗試在XAML中切換Command和CommandParameter的順序,以查看是否在定義的參數之前設置了Command,但是按鈕命令對象仍然為NULL。 有沒有解決方案跳給任何人? 提前謝謝了!

ViewModel:

public partial class UsersViewModel : INotifyPropertyChanged
{
    public RelayCommand<UserViewModel> editButton_Click_Command { get; set; }

    public UsersViewModel()
    {
        editButton_Click_Command = new RelayCommand<UserViewModel>(OneditButton_Click_Command);

        this.Users = new ObservableCollection<UserViewModel>();

        this.Users.Add(new UserViewModel() { FirstName = "John", LastName = "Doe", EMail = "JohnDoe@yahoo.com", EndDate = new DateTime(2016,2,1), Position = "Developer", UserID = 0 });
    }

    private ObservableCollection<UserViewModel> _Users;
    public ObservableCollection<UserViewModel> Users
    {
        get { return _Users; }
        set { _Users = value; NotifyPropertyChanged("Users"); }
    }

    private void OneditButton_Click_Command(UserViewModel obj)
    {
        //Parameter object is always NULL here!!!
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

XAML:

<Window x:Name="Base_V"
    x:Class="DbEntities.UsersWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DbEntities"
    xmlns:ViewModels="clr-namespace:DbEntities"
    xmlns:staticData="clr-namespace:DbEntities"
    mc:Ignorable="d"
    Title="UsersWindow" Height="Auto" Width="900">
    <Window.Resources>
        <staticData:PositionsList x:Key="PositionsList" />
    </Window.Resources>
    <Window.DataContext>
        <ViewModels:UsersViewModel/>
    </Window.DataContext>
    <Grid>
        <FrameworkElement x:Name="dummyElement" Visibility="Collapsed" />
        <DataGrid Name="DataGrid1" ItemsSource="{Binding Users}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
              ColumnWidth="*" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Command="{Binding DataContext.editButton_Click_Command, ElementName=Base_V}" CommandParameter="{Binding}" >Edit</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="User ID" Binding="{Binding UserID}" IsReadOnly="True" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="E-Mail" Binding="{Binding EMail}" />
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <Label Content="Position" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{StaticResource PositionsList}" SelectedItem="{Binding Position}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="End Date" Binding="{Binding EndDate, StringFormat={}{0:MM/dd/yyyy}}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

RelayCommand來自此處: http ://www.kellydun.com/wpf-relaycommand-with-parameter/

編輯:建議將UserViewModel發布,以查看是否可以在此處或在其他問題中解決該問題。 還有一些從數據庫中提取的測試用戶。

UserViewModel:

public class UserViewModel : INotifyPropertyChanged
{
    private string _FirstName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; NotifyPropertyChanged("FirstName"); }
    }

    private string _LastName;
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; NotifyPropertyChanged("LastName"); }
    }

    private string _EMail;
    public string EMail
    {
        get { return _EMail; }
        set { _EMail = value; NotifyPropertyChanged("EMail"); }
    }

    private int _UserID;
    public int UserID
    {
        get { return _UserID; }
        set { _UserID = value; NotifyPropertyChanged("UserID"); }
    }

    private string _Position;
    public string Position
    {
        get { return _Position; }
        set { _Position = value; NotifyPropertyChanged("Position"); }
    }

    private DateTime? _EndDate;
    public DateTime? EndDate
    {
        get { return _EndDate; }
        set { _EndDate = value; NotifyPropertyChanged("EndDate"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

在主頁上:

UsersViewModel Usersvm = new UsersViewModel();
Usersvm.Users = new ObservableCollection<UserViewModel>();
DbEntities db = new DbEntities();
var pulledUsers = db.uspGetNonAdminUsers().ToList();
foreach (var result in pulledUsers)
{
    var pulledUser = new UserViewModel
    {
        FirstName = result.FirstName,
        LastName = result.LastName,
        EMail = result.Email,
        UserID = result.UserID,
        Position = result.Position,
        EndDate = result.EndDate
    };
    Usersvm.Users.Add(pulledUser);
}
new UsersWindow(Usersvm).Show();

UsersWindow:

public partial class UsersWindow : Window
{
    public UsersWindow(UsersViewModel uvm)
    {
        InitializeComponent();
        DataContext = uvm;
    }
}

您需要做一些事情:

將您的屬性更新為object類型:

   public RelayCommand<object> editButton_Click_Command { get; set; }

更新您的實例以使用object

editButton_Click_Command = new RelayCommand<object>(OneditButton_Click_Command);

然后,將事件處理程序更新為具有param object並進行相應的轉換。

private void OneditButton_Click_Command(object obj)
{
    var associatedViewModel = obj as UserViewModel;
    if (associatedViewModel != null)
    {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM