首先,我应该说我是一名新手程序员,所有帮助都表示赞赏。我目前正在开发一个wpf应用程序,我想在其中拥有一个带有标签和内容控件的usercontrol,可以根据从欢迎视图中选择的按钮进行更新。像这样
<Window x:Class="ContentControl.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ContentControl.ViewModels"
xmlns:views="clr-namespace:ContentControl.Views"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:ScreenViewModel}">
<views:ScreenView DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:WelcomeViewModel}">
<views:WelcomeView DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MeetingRoomViewModel}">
<views:MeetingRoomView DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:DashboardViewModel}">
<views:DashboardView />
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<Label>This Is My Label</Label>
<ContentControl x:Name="MainPanel" Content="{Binding Path=Content}"
MinHeight="200"
MinWidth="200"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Center"
Focusable="False">
</ContentControl>
</StackPanel>
</Grid>
</Window>
背后的代码:
public MainWindow()
{
InitializeComponent();
DataContext = this;
MainPanel.Content = new WelcomeView();
MainPanel.Content = this.MainPanel.Content;
}
}
这是WelcomeViewModel:
internal class WelcomeViewModel : BaseViewModel
{
private MainWindowViewModel _mainWindowVm;
private RelayCommand<string> _viewChangedCommand;
public ICommand ViewChangedCommand
{
get { return _viewChangedCommand ?? (_viewChangedCommand = new RelayCommand<string>(OnViewChanged)); }
}
public event EventHandler ViewChanged;
private void OnViewChanged(string view)
{
EventHandler handler = ViewChanged;
if (handler != null) handler(view, EventArgs.Empty);
}
public MainWindowViewModel MainWindowVm
{
get { return _mainWindowVm; }
set
{
_mainWindowVm = value;
OnPropertyChanged("MainViewModel");
}
}
public WelcomeViewModel()
{
MainWindowVm = new MainWindowViewModel();
ViewChanged += MainWindowVm.ViewChanged;
}
}
最后我的welcome.xaml
<UserControl x:Class="ContentControl.Views.WelcomeView"
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:vm="clr-namespace:ContentControl.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<vm:WelcomeViewModel />
</UserControl.DataContext>
<Grid Background="red">
<Grid.RowDefinitions >
<RowDefinition Height="25*" />
<RowDefinition Height="50*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Fill="Green"/>
<DockPanel Grid.Row="1" HorizontalAlignment="Center" Background="White">
<Button Height="50" Width="50" Margin="5" Content="DASH" Command="{Binding ViewChangedCommand}" CommandParameter="Dashboard"/>
<Button Height="50" Width="50" Margin="5" Content="ROOM" Command="{Binding ViewChangedCommand}" CommandParameter="MeetingRoom"/>
<Button Height="50" Width="50" Margin="5" Content="SCREEN" Command="{Binding ViewChangedCommand}" CommandParameter="Screen" />
</DockPanel>
<Rectangle Grid.Row="2" Fill="Blue"/>
</Grid>
</UserControl>
所以问题是当ViewChange事件被触发时,它在MainWindowViewModel中可见,但是当它使用PropertyEventHandler时(如下所示),PropertyChanged始终为null。
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
答案 0 :(得分:1)
嗯,这里有一些WPF,Bindings和Mvvm错误...... 首先,你为什么要这样做:
MainPanel.Content = this.MainPanel.Content;
与以下内容相同:
MainPanel.Content = MainPanel.Content;
这条线是不必要的。
你说的第二个原因:
Content="{Binding Path=Content}"
然后你在你的代码中设置:
MainPanel.Content = new WelcomeView();
这里可能有一个概念错误:默认情况下设置绑定时,将对控件本身的DataContext(本例中为UserControl)执行此绑定。 好的,为了解决这个问题并使用Mvvm,让我们保持绑定:
Content="{Binding Path=Content}"
但现在我们需要设置UserControl datacontext:
MainPanel.DataContext = new MainPanelViewModel();
现在我们需要在名为Content的MainPanelViewModel中创建一个属性。 在此属性中,您将设置要在ContentControl.Content中显示的内容。 (在本例中为WelcomeViewModel和您想要的任何内容)
希望这个答案有助于您从wpf和mvvm开始。这是一个很棒的平台。
答案 1 :(得分:0)
好。您可以解决的错误:
或许可以更好地解释您的问题,我可以提供更多信息。