madaxe2020
会员
- 已加入
- 2020年9月7日
- 留言内容
- 17
- 编程经验
- 5-10
我不会在选择按钮并将记录添加到组合框和datagridview后清除文本框的行为
我使用MVVM作为我的模式,并将文本框绑定到我要设置为string.Empty的属性上,在创建新记录后,该文本框不会更新。
谢谢
马达克斯
我使用MVVM作为我的模式,并将文本框绑定到我要设置为string.Empty的属性上,在创建新记录后,该文本框不会更新。
谢谢
马达克斯
XAML:
<Window x:Class="WPFBinding.MainWindow"
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:WPFBinding"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="530">
<Grid>
<StackPanel Width="500">
<TextBox x:Name="TextInput" Text="{Binding myContinentName,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
UpdateSourceTrigger=PropertyChanged}"/>
<Button Command="{Binding ButtonCommand}"
CommandParameter="{Binding ElementName=TextInput,
Path=Text}"
Content="Add to ComboBox"/>
<ComboBox ItemsSource="{Binding myMessages}"/>
<DataGrid x:Name="DGD_UserList"
AutoGenerateColumns="False"
ItemsSource="{Binding myContinents,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
UpdateSourceTrigger=PropertyChanged}"
Margin="0,5,5,5"
ColumnWidth="*"
Background="#FF9F9E9E" HorizontalAlignment="Right" Width="481">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding continent_name}" Header="Continent Name" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding modifying_user_id}" Header="Modifying User" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding modifying_date}" Header="Modification Date" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
Relay:
using System;
using System.Windows.Input;
namespace WPFBinding
{
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new NullReferenceException("execute");
}
else
{
this._execute = execute;
this._canExecute = canExecute;
}
}
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return this._canExecute == null ? true : this._canExecute(parameter);
}
public void Execute(object parameter)
{
this._execute.Invoke(parameter);
}
}
}
View Model Base:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WPFBinding.Models.Base
{
public class ViewModelBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged 会员
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
View 模型:
using System;
using System.Collections.ObjectModel;
using WPFBinding.Models;
using WPFBinding.Models.Base;
namespace WPFBinding.ViewModels
{
public class DataViewModel : ViewModelBase
{
public string myContinentName { get; set; }
public ObservableCollection<Continent> myContinents { get; set; }
public ObservableCollection<string> myMessages { get; private set; }
public RelayCommand ButtonCommand { get; private set; }
public DataViewModel()
{
this.myMessages = new ObservableCollection<string>() { };
this.myContinents = new ObservableCollection<Continent>() { };
this.ButtonCommand = new RelayCommand(AddtoComboBox, ComboboxCanAdd);
}
public void AddtoComboBox(object message)
{
if (this.myMessages.Contains((string)message) == false)
{
Continent continent = new Continent((string)message, 1, DateTime.Now);
this.myContinents.Add(continent);
this.myMessages.Add((string)message);
this.myContinentName = string.Empty;
}
}
public bool ComboboxCanAdd(object message)
{
if (string.IsNullOrWhiteSpace((string)message) == true || this.myMessages.Contains((string)message) == true)
{
return false;
}
else
{
return true;
}
}
}
}
Model:
using System;
namespace WPFBinding.Models
{
public class Continent
{
public string continent_name { get; set; }
public int modifying_user_id { get; set; }
public DateTime modifying_date { get; set; }
public Continent(string ContinentName, int ModifyingUserId, DateTime ModifyingDate)
{
this.continent_name = ContinentName;
this.modifying_user_id = ModifyingUserId;
this.modifying_date = ModifyingDate;
}
}
}