首先,我应该说我是一个新手程序员,非常感谢大家的帮助。我目前正在开发一个wpf应用程序,在该应用程序中,我希望有一个具有标签和内容控件的用户控件,它可以根据从欢迎视图中选择的按钮进行更新。就像这样
<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总是空的。
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}发布于 2012-09-22 02:07:53
好了,这里有一些WPF、绑定和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数据上下文:
MainPanel.DataContext = new MainPanelViewModel();现在我们需要在MainPanelViewModel中创建一个名为Content的属性。在此属性中,您将设置要在ContentControl.Content中显示的内容。(在本例中是WelcomeViewModel和您想要的任何东西)
希望这个答案能帮助你开始使用wpf和mvvm。这是一个很棒的平台。
发布于 2012-09-22 02:02:55
好吧。你可以修正的错误:
也许可以更好地解释一下你的问题,我可以给你提供更多的信息。
https://stackoverflow.com/questions/12535122
复制相似问题