【发布时间】:2020-09-11 21:18:11
【问题描述】:
我正在使用 ViewModel-first aprroach。在 MainWindow.xaml 中,我设置了 ContentControl,通过单击 MenuItem 来显示我的 UserControls(视图)。当我第一次点击显示 UserControl 时,一切正常。
但是当我单击相同的 MenuItem 再次打开它时,我的 UserControl 再次显示但不再加载,导致没有刷新绑定。将我的 ContentControl 的 Content 设置为 null 并不能解决问题。
我的整个设置是这样的:
1.) App.xaml 资源
<!--DataContext for MainWindow.xaml-->
<ViewModels:MainWindowViewModel x:Key="Main_VM"/>
<!--DataTemplate for UserControl-->
<DataTemplate DataType="{x:Type ViewModels:MyViewModel}">
<Views:MyView />
</DataTemplate>
2.) MainWindow.xaml,我的 ContenControl 所在的位置
<Window x:Class="My.Views.MainWindowView"
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"
DataContext="{StaticResource Main_VM}">
<Grid>
<!--Menu which opens view on command-->
<Menu VerticalAlignment="Top" IsMainMenu="True" >
<MenuItem Header="My View" Command="{Binding Show_View}" CommandParameter="1"/>
</Menu>
<ContentControl Content="{Binding Display_View}" />
<!--And all other controls, like Menu for opening views on click...-->
</Grid>
</Window>
3.) Mainwindow.xaml 的 ViewModel(继承自 BaseViewModel)
public class MainWindowViewModel : BaseViewModel
{
public MainWindowViewModel()
{
//Command for displaying Views
Show_View = new Relay_Command(Open_view, null);
}
public ICommand Show_View { get; set; }
private BaseViewModel _display_view;
public BaseViewModel Display_View
{
get { return _display_view; }
set { _display_view = value; OnPropertyChanged(); }
}
private void Open_view(object parameter)
{
Display_View = null; //This doesn't help at all!!!
switch (parameter)
{
case "1":
Display_View= new MyViewModel();
break;
}
}
}
4.) 还有我的 UserControl.xaml
<UserControl x:Class="MyProject.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<!--Event-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<!--Calling a method on Load (firing only first tme !!)-->
<ei:CallMethodAction MethodName="MethodForRetrievingData" TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<!--Controls in UserControl for binding etc...-->
</Grid>
</UserControl>
我尝试过调试,但如告知的那样,UserControl 的加载事件只发生一次。我对此一无所知,看来我的设计有缺陷。
这可能是什么问题,也许我在 UserControl 本身上缺少 NotifyProperty 之类的东西?
【问题讨论】:
-
你能把
Display_View绑定到content control的代码贴出来吗?根据您当前发布的代码`` 我看到 Show_View 有一个绑定 -
@user1672994,感谢您注意到,这是一个错字,很抱歉 - Display_View 是绑定到 ContentControl 的。我在 ManWindow.xaml 中编辑了问题,我添加了一个菜单控件,它也可以打开视图 - 它绑定到 Show_View。