WPFの新機能はこちら。私が構築しているこの最初のアプリケーションは、ButtonCommandでRelayCommandとDataGridを使用して、新しいユーザーを編集または入力しています。開いている行に値が入力された新しいユーザーの場合、OneditButtonCommandは押されたときに呼び出されますが、パラメーターとして渡される項目は常にnullです。私はコマンドが定義されたパラメータの前に設定されているかどうかを確認するためにXAMLのCommandとCommandParameterの順序を入れ替えようとしましたが、ボタンのコマンドオブジェクトはまだNULLでした。どんな解決策も誰にも飛びついていますか?事前に多くの感謝!ボタンコマンドでWPFの新しいDataGrid行パラメータ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 = "[email protected]", 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/
EDIT: それはUserViewModelはに掲載されていることが示唆されました問題がここで解決できるか、別の問題で解決できるかどうかを確認してください。データベースから取り出された2人のテストユーザーもいます。
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'に変更して、別の型を取得してみてください。 – benPearce
ありがとう@benPearce。プライベートvoid OneditButton_Click_Commandのパラメータタイプをobjectに変更しました。このボタンをクリックすると、関数にヒットしません。それが正しい変更でしたか? – jle
多くの情報を収集するのは単なる推測だったとは言えませんが、RelayCommand <>もオブジェクトに変更する必要があります。 – benPearce