- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我现在已经为我的问题苦苦挣扎了一个星期,但我无法解决它。我正在编写一个只有一个窗口 (MainView) 的 MVVM WPF 应用程序。在此 Mainview
中,我想在需要时加载不同的 UserControl
。在 Application-Startup
中,我正在加载 MainViewModel
。在 MainViewModel
的构造函数中,我正在加载第一个 ViewModel
(LoginViewModel
)。我的 MainView.xaml
的原因是它向我展示了我想要的 Login-User-Control
。所以到目前为止一切都很好。在 ActivePanel-class
中,我正在保存 CurrentView
,因为在我的 MainView.xaml
中,我正在绑定(bind)到我的 CurrentView
用于 ContentControl
。因此,尽管我的 CurrentView
的 NotifyPropertyChanged
方法正在运行,但除 View 更改外一切正常。我在想,我的错误在于 xaml
(DataBinding)。希望你们能帮助我。
这是我的 MainView.xaml
,我想在其中加载不同的数据模板。正如我之前所说:通过 MainViewModel
的构造函数加载 LoginViewModel
正在运行。更改为其他 VieModels
也能正常工作,但是 ContentControl
的 DataBinding 是这里的大问题。
<Window x:Class="App.View.MainView"
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:App.View"
mc:Ignorable="d"
xmlns:viewmodels="clr-namespace:App.ViewModels"
xmlns:views="clr-namespace:App.View"
xmlns:helper="clr-namespace:App.Helper"
Title="Betrooms" Height="500" Width="350">
<Window.Resources>
<DataTemplate x:Name="LoginUCTemplate" DataType="{x:Type viewmodels:LoginViewModel}">
<views:LoginUC DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate x:Name="RegUCTemplate" DataType="{x:Type viewmodels:RegViewModel}">
<views:RegUC DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate x:Name="HomeUCTemplate" DataType="{x:Type viewmodels:HomeViewModel}">
<views:HomeUC DataContext="{Binding}"/>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<viewmodels:ActivePanel/>
</Window.DataContext>
<Grid>
<ContentControl Content="{Binding CurrentView, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
</Grid>
</Window>
这是我的 ActivePanel
类,我在其中保存关于哪个 ViewModel
是事件面板的信息。 CurrentView
是我将内容控件绑定(bind)到的属性。
namespace APP.ViewModels
{
public class ActivePanel : NotifyPropertyChanged
{
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
if (value != _currentView)
{
_currentView = value;
OnPropertyChanged("CurrentView");
}
}
}
}
}
这是我的 MainViewModel
:
namespace App.ViewModels
{
public class MainViewModel : ActivePanel
{
private LoginViewModel _loginViewModel;
public MainViewModel()
{
_loginViewModel = new LoginViewModel();
CurrentView = _loginViewModel;
}
}
}
这是我的 LoginViewModel
,我在其中通过操作更改 CurrentView
的值:
namespace App.ViewModels
{
public class LoginViewModel : ActivePanel
{
#region Member
private string _username;
private string _password;
bool login = false;
private HomeViewModel _homeViewModel;
private RegViewModel _regViewModel;
UserModel User = new UserModel();
#endregion
#region Properties
public ICommand RegActionCommand { get; set; }
public ICommand LogActionCommand { get; set; }
public string GetUsername
{
get { return _username; }
set
{
if (value != _username)
{
_username = value;
OnPropertyChanged("GetUsername");
}
}
}
public string GetPassword
{
get { return _password; }
set
{
if (value != _password)
{
_password = value;
OnPropertyChanged("GetPassword");
}
}
}
#endregion
#region Constructor
public LoginViewModel()
{
this.RegActionCommand = new RelayCommand(RegAction);
this.LogActionCommand = new RelayCommand(LogAction);
}
#endregion
#region Button-Action
private void LogAction(object obj)
{
_homeViewModel = new HomeViewModel();
CurrentView = _homeViewModel;
}
private void RegAction(object obj)
{
_regViewModel = new RegViewModel();
CurrentView = _regViewModel;
}
#endregion
}
}
我希望我的问题是可以理解的:ContenControl
绑定(bind)设置为 CurrentView
但 ContentControl
永远不会改变,尽管 的属性>CurrentView
正在改变。
谢谢大家。干杯,保罗。
最佳答案
在您的命令处理程序中,您正在更改 CurrentView
你的属性(property)LoginViewModel
. ContentControl
的 DataContext
是 MainViewModel
不过,它的内容绑定(bind)到 CurrentView
MainViewModel
的属性(property).
您需要设置 MainViewModel
命令处理程序中的属性。有不同的方法可以实现这一点,例如,您可以将构造函数参数添加到 LoginViewModel
传递对 MainViewModel
的引用.您可以保存此引用,然后在您的命令处理程序中访问它。
另一种可能性是引发事件或从您的 LoginViewModel
中的命令发送消息并在 MainViewModel
中处理.这将减少 ViewModel 之间的耦合,但根据您使用的机制和库,它可能会稍微复杂一些。
关于c# - NotifyPropertyChanged 和 ContentControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52262973/
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: typesafe NotifyPropertyChanged using linq expressions 我正在开
我刚刚根据 Rachel Lim 的 blog 实现了我的业务逻辑验证。 . 一切都运行良好,直到我决定在绑定(bind)到 的 View 中放置一个触发器。有效像这样的属性(property):
测试此属性的最佳方法是什么: public string ThreadId { get { return _threadId; } set {
我有以下 View 模型 [NotifyPropertyChanged] public class ActivateViewModel { public string Password { g
我正在尝试为 ListBox 捕获 SelectedItem,然后为 Button 触发数据模板单击事件。我在 notifypropertychanged 事件处理程序中放置了一个断点,但它从未触发。
我正在查看 INotifyPropertyChanged 中的 NotifyPropertyChanged() 并注意到在 Microsoft 的示例中,例如此处: http://msdn.micro
如果我更新的属性在绑定(bind)控件的调度程序以外的线程上抛出 NotifyPropertyChanged,更新是否会被强制编码到此调度程序? BackgrounWorker.Run() => {
您好,我有一个 DataGridCheckBoxColumn,我希望它在用户选中或取消选中 DataGridCheckBoxColumn 时立即在基础绑定(bind)对象上通知 propertycha
似乎有些人在 Silverlight 中更新文本 AutoCompleteBox 时遇到问题,遗憾的是我已经加入了这个行列。 我有一个像这样的名为 EditableCombo 的派生类; publ
我的应用程序具有定期数据库同步功能。每当发生同步时,所有输入控件的值都会重置为数据库中的当前值。 但是,当在 TextBox 中键入长文本时发生同步事件时,这非常不方便。 期望的行为是输入控件的值不设
我有一个 UserControl,它包含 bool 属性 A。在包含该 UserControl 的主窗口中,我必须根据 A 的值启用/禁用按钮。我试图像这样将 A 设置为公开和绑定(bind)按钮:
我现在已经为我的问题苦苦挣扎了一个星期,但我无法解决它。我正在编写一个只有一个窗口 (MainView) 的 MVVM WPF 应用程序。在此 Mainview 中,我想在需要时加载不同的 UserC
我正在使用 Android 的数据绑定(bind)库。我的数据对象扩展了 BaseObservable。 public static class SimpleData extends BaseObse
在我的新 WPF 应用程序中,我重用了一个模型类。在该模型类中,所有属性都在其 setter 中触发 NotifyPropertyChanged。在我的应用程序中,我并没有真正为单个属性触发 INPC
是否有类似 INotifyPropertyChanged 的接口(interface),其中事件参数包含正在更改的属性的旧值,或者我是否必须扩展该接口(interface)来创建一个接口(inte
我有一个组件,其中包含一个子组件列表(在父组件中使用 yield 绘制): parent-component for item in items child-component item=
表格 Build your own MVVM我有以下代码可以让我们进行类型安全的 NotifyOfPropertyChange 调用: public void NotifyOfPropertyChan
这是一个简化的示例,但很好地说明了我的问题。我有一个 PIN 码输入,带有数字按钮(1、2、3、4)。我的按钮上的操作应该设置一个属性 pinValue ,该属性将传递到包装的组件 pin-input
我对 INotifyPropertyChanged 的 PostSharp 实现没有什么问题。 PostSharp 在编译时添加了 PropertyChangedEventHandler Proper
我对 WPF 非常熟悉,但有一件事困扰着我。我正在使用 ReactiveUI 引发 INotifyPropertyChanged 事件。我有两个地方与此类似: public UiModel UiMo
我是一名优秀的程序员,十分优秀!