如何设置文本框';s值在MVVM[C#]中将其值传输到gridview时为空
我希望当点击ADD按钮时,文本框的值转移到gridview,文本框的值将被设置为空。如何做到这一点如何设置文本框';s值在MVVM[C#]中将其值传输到gridview时为空,c#,wpf,gridview,mvvm,datagrid,C#,Wpf,Gridview,Mvvm,Datagrid,我希望当点击ADD按钮时,文本框的值转移到gridview,文本框的值将被设置为空。如何做到这一点 My View(XAML file) <Window x:Class="EEEMVVM.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:he
My View(XAML file)
<Window x:Class="EEEMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helper="clr-namespace:EEEMVVM.Helper"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mo="clr-namespace:EEEMVVM.Model"
xmlns:vm="clr-namespace:EEEMVVM.View_Model" mc:Ignorable="d"
Title="MainWindow" Height="387" Width="567">
<Window.Resources>
<vm:ShowStudentInfo x:Key="ShowStudentInfo"/>
<vm:NewStudentViewModelBase x:Key="NewStudentViewModelBase"/>
<mo:Student x:Key="Student"/>
</Window.Resources>
<Grid Height="263" Width="476">
<Grid.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="48*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="18*" />
</Grid.ColumnDefinitions>
<Label Height="28" Margin="0,7,0,0" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Content="Name" />
<TextBox Grid.Column="1" Grid.Row="0" Height="23" Margin="6,12,0,0"
HorizontalAlignment="Left" Name="txtbox_name" VerticalAlignment="Top"
Width="120"
Text="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentName,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
<Label Grid.Column="2" Height="28" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Content="Roll No" Margin="0,10,0,0" Grid.RowSpan="2" />
<TextBox Grid.Column="3" Grid.Row="0" Height="23" Margin="6,12,0,0"
HorizontalAlignment="Left" Name="txtbox_rollno" VerticalAlignment="Top"
Width="120" MaxLength="7" helper:TextBoxHelpers.IsNumeric="True"
Text="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentRollNo,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
<Label Grid.Row="1" Height="28" Margin="0,7,0,0" HorizontalAlignment="Left" Name="label3" VerticalAlignment="Top" Content="Email" />
<TextBox Grid.Column="1" Grid.Row="1" Height="23" Margin="6,12,0,0"
HorizontalAlignment="Left" Name="txtbox_email" VerticalAlignment="Top"
Width="120"
Text="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentEmail,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"/>
<Label Grid.Column="2" Grid.Row="1" Height="28" Margin="0,7,0,0" HorizontalAlignment="Left" Name="label4" VerticalAlignment="Top" Content="Contact No"/>
<TextBox Grid.Column="3" Grid.Row="1" Height="23" Margin="6,12,0,0"
HorizontalAlignment="Left" Name="txtbox_contactno" VerticalAlignment="Top"
Width="120" MaxLength="10" helper:TextBoxHelpers.IsNumeric="True"
Text="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentContactNo,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"/>
<ListView ItemsSource="{Binding Source={StaticResource NewStudentViewModelBase}, Path=Students}" Grid.Row="3" Grid.ColumnSpan="6" Width="393" Margin="28,23,55,25" HorizontalAlignment="Center" Background="White" Height="60">
<ListView.View >
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentName}" Width="90"/>
<GridViewColumn Header="Roll No" DisplayMemberBinding="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentRollNo}" Width="90"/>
<GridViewColumn Header="Email" DisplayMemberBinding="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentEmail}" Width="90"/>
<GridViewColumn Header="Contact No" DisplayMemberBinding="{Binding Source={StaticResource NewStudentViewModelBase}, Path=StudentContactNo}" Width="90"/>
</GridView>
</ListView.View>
</ListView>
<Button Command="{Binding Source={StaticResource NewStudentViewModelBase}, Path=InsertDataCommand}" IsEnabled="{Binding IsValidSave}" Grid.Column="1" Grid.Row="4" Content="Add" Height="26" Margin="37,30,20,11" Name="button1" Width="75" />
<Button Content="Submit" Grid.Column="3" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="24,33,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>
}您的InsertDataCommand应该是这样的:
private RelayCommand _insertDataCommand;
public RelayCommand InsertDataCommand
{
get
{
return _insertDataCommand
?? (_insertDataCommand = new RelayCommand(
() =>
{
Students.Add(new Student
{
StudentRollNo = this.StudentRollNo,
StudentName = this.StudentName,
StudentEmail = this.StudentEmail,
StudentContactNo = this.StudentContactNo
});
StudentRollNo = String.Empty;
StudentName = String.Empty;
StudentEmail = String.Empty;
StudentContactNo = String.Empty;
}));
}
}
由于
Students
属性是一个ObservableCollection
(这意味着实现INotifyCollectionChanged
,更改后的属性将显示在UI中,无需触发或设置任何其他内容),对于其他四个属性,将其值设置为String.Empty
将触发NotifyPropertyChanged
方法,并且更改也将传播到UI此错误即将出现:委托“System.Action”不接受0参数我正在使用GalaSoft.MvvmLight.Command通过Nugget使用“安装包MvvmLight”并添加“使用GalaSoft.MvvmLight.Command”;“通过将上述内部命令代码放入Save方法中,您可以实现同样的效果!……而且不要忘记实例化ObservableCollection@ManishPant命令的execute函数以一个对象作为参数。您可以将(obj)=>{…}而不是()=>{…}.@Joseph通过这样做,文本框被清除,但空值存储在gridview中
namespace EEEMVVM.Model
{
class Student:INotifyProperty
{
public string StudentRollNo
{
get; set;
}
public string StudentName
{
get; set;
}
public string StudentEmail
{
get; set;
}
public string StudentContactNo
{
get; set;
}
public void Save()
{
}
}
private RelayCommand _insertDataCommand;
public RelayCommand InsertDataCommand
{
get
{
return _insertDataCommand
?? (_insertDataCommand = new RelayCommand(
() =>
{
Students.Add(new Student
{
StudentRollNo = this.StudentRollNo,
StudentName = this.StudentName,
StudentEmail = this.StudentEmail,
StudentContactNo = this.StudentContactNo
});
StudentRollNo = String.Empty;
StudentName = String.Empty;
StudentEmail = String.Empty;
StudentContactNo = String.Empty;
}));
}
}