c# - 用户控件不更新文本框 mvvm WPF C#
问题描述
对不起这么多代码。但不幸的是,有太多的联系。这就是为什么我不得不插入这么多代码。我试图将其保持在最低限度。我希望这已经足够了。
我的问题:正如您在此处看到的,有一个按钮可以选择计算机上的路径。还有一个文本框再次显示路径。
XAML 用户控制代码:
<DockPanel Grid.Row="1" Grid.Column="1">
<Button
Content="Repo Pfad"
Command="{Binding SelectRepoPathCommand}"/>
</DockPanel>
<DockPanel Grid.Row="1" Grid.Column="2">
<TextBox Text="{Binding repoPath, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</DockPanel>
这里代码存储在用户的配置中。这样他就不必在下一次开始时一次又一次地进入路径。小附加信息:由于配置路径保存成功,第二次选择路径后测试框将更新。这是因为第二次显示之前保存的路径。
视图模型:
class BuildToolViewModel : ObservableObject
{
private string _repoPath;
public string repoPath
{
get
{
if (Properties.Settings.Default.repoPath != null)
{
return Properties.Settings.Default.repoPath;
}
else
{
return _repoPath;
}
}
set
{
_repoPath = value;
OnPropertyChanged("repoPath");
Properties.Settings.Default.repoPath = _repoPath;
Properties.Settings.Default.Save();
}
}
public RelayCommand SelectRepoPathCommand{ get; private set; }
#endregion #Properties
#region ctor
public BuildToolViewModel()
{
SelectRepoPathCommand = new RelayCommand(SelectRepoPath);
}
#endregion //ctor
#region Methods
public void SelectRepoPath(object sender)
{
repoPath = explorerDialog.OpenFileDialogPath();
}
}
这是继承自 INotifyPropertyChanged 的 ObservableObject。
可观察对象(INotifyPropertyChanged):
class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
这是继承自 ICommand 的 RelayCommand。
中继命令(ICommand):
class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region ctor
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion //ctor
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return _canExecute == null ? true : _canExecute(parameters);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
#endregion // ICommand Members
}
这里还是喜欢在 MainWindowViewModel 中用 ICommand 实现的。
MainWindowViewModel(可观察对象):
class MainWindowViewModel : ObservableObject
{
private ICommand _changePageCommand;
private IPageViewModel _currentPageViewModel;
private List<IPageViewModel> _pageViewModels;
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand(
p => ChangeViewModel((IPageViewModel)p),
p => p is IPageViewModel);
}
return _changePageCommand;
}
}
解决方案
修复你的属性 repoPatch 代码:
private string _repoPath;
public string repoPath
{
get
{
if (string.IsNullOrEmpty(_repoPath))
{
_repoPath= Properties.Settings.Default.repoPath;
}
return _repopath
}
....................
............
推荐阅读
- database - 大查询数据过期
- reactjs - 服务器启动并运行,但网页未在 heroku 本地呈现
- uwp - XDE:010 模拟器无法创建差异磁盘
- python - Read_Csv() 没有给出预期的输出,如果我更新了 csv 中的任何单元格
- javascript - 取消选择并选择事件
- node.js - Next.js 的内存问题
- javascript - 为什么我的 Ajax 无法将数据保存到数据库中?Laravel 与 Ajax
- sql - 从其他表列检索记录
- javascript - 空白屏幕 - Matter.js
- druid - Druid Concat String 和 Measure 聚合