gpt4 book ai didi

c# - NotifyPropertyChanged 和 ContentControl

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:06 25 4
gpt4 key购买 nike

我现在已经为我的问题苦苦挣扎了一个星期,但我无法解决它。我正在编写一个只有一个窗口 (MainView) 的 MVVM WPF 应用程序。在此 Mainview 中,我想在需要时加载不同的 UserControl。在 Application-Startup 中,我正在加载 MainViewModel。在 MainViewModel 的构造函数中,我正在加载第一个 ViewModel (LoginViewModel)。我的 MainView.xaml 的原因是它向我展示了我想要的 Login-User-Control。所以到目前为止一切都很好。在 ActivePanel-class 中,我正在保存 CurrentView,因为在我的 MainView.xaml 中,我正在绑定(bind)到我的 CurrentView 用于 ContentControl。因此,尽管我的 CurrentViewNotifyPropertyChanged 方法正在运行,但除 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)设置为 CurrentViewContentControl 永远不会改变,尽管 的属性>CurrentView 正在改变。

谢谢大家。干杯,保罗。

最佳答案

在您的命令处理程序中,您正在更改 CurrentView你的属性(property)LoginViewModel . ContentControlDataContextMainViewModel不过,它的内容绑定(bind)到 CurrentView MainViewModel 的属性(property).

您需要设置 MainViewModel命令处理程序中的属性。有不同的方法可以实现这一点,例如,您可以将构造函数参数添加到 LoginViewModel传递对 MainViewModel 的引用.您可以保存此引用,然后在您的命令处理程序中访问它。

另一种可能性是引发事件或从您的 LoginViewModel 中的命令发送消息并在 MainViewModel 中处理.这将减少 ViewModel 之间的耦合,但根据您使用的机制和库,它可能会稍微复杂一些。

关于c# - NotifyPropertyChanged 和 ContentControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52262973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com