new to WPF here. This first app I'm building is using a RelayCommand and DataGrid with ButtonCommand to edit or enter a new user. For a new user with values entered into the open row, the OneditButtonCommand is being called when pressed, but the Item being passed as a parameter is always null. I tried switching the order of Command and CommandParameter in the XAML to see if the Command was being set before a defined parameter, but the button command object was still NULL. Does any solution jump out to anyone? Many thanks in advance!
The 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));
}
}
}
The 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>
The RelayCommand was taken from here: http://www.kellydun.com/wpf-relaycommand-with-parameter/
EDIT: It was suggested that the UserViewModel be posted to see if the issue can be resolved here or in another question. There are a couple of test users pulled from a database as well.
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));
}
}
}
On the home page:
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();
The UsersWindow:
public partial class UsersWindow : Window
{
public UsersWindow(UsersViewModel uvm)
{
InitializeComponent();
DataContext = uvm;
}
}
You need to do a few things:
Update your property to be type object
:
public RelayCommand<object> editButton_Click_Command { get; set; }
update your instantiation to use object
editButton_Click_Command = new RelayCommand<object>(OneditButton_Click_Command);
then, update your event handler to have a param object
and cast accordingly.
private void OneditButton_Click_Command(object obj)
{
var associatedViewModel = obj as UserViewModel;
if (associatedViewModel != null)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.