Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf内容控制_Wpf_Mvvm_Binding_Contentcontrol - Fatal编程技术网

Wpf内容控制

Wpf内容控制,wpf,mvvm,binding,contentcontrol,Wpf,Mvvm,Binding,Contentcontrol,新问题: 我正在尝试创建一个简单的导航欢迎页面,类似于你在ipad上看到的页面 我的主窗口有一个标题栏(不会改变),其余的将是一个容器,根据事件显示不同的内容 因此,我的问题是,如何绑定容器(contentcontrol)以显示其他视图(最初显示welcomeview),然后如果用户单击欢迎视图中的按钮,内容控件将显示他们选择的视图 我目前的欢迎页面为: <UserControl x:Class="ContentControl.Views.WelcomeView" xml

新问题:

我正在尝试创建一个简单的导航欢迎页面,类似于你在ipad上看到的页面

我的主窗口有一个标题栏(不会改变),其余的将是一个容器,根据事件显示不同的内容

因此,我的问题是,如何绑定容器(contentcontrol)以显示其他视图(最初显示welcomeview),然后如果用户单击欢迎视图中的按钮,内容控件将显示他们选择的视图

我目前的欢迎页面为:

<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>
我如何将这些连接起来,使它们相互协作,以便将WelcomeView显示为contentcontrol的内容,然后在按下按钮时,将contentcontrol更改为所选内容


再次抱歉,我是MVVM和WPF的新手,这些绑定正在干扰我的大脑。

仪表板、房间、屏幕的按钮应该在主窗口上。ViewChangedCommand应位于MainWindowViewModel上。内容将通过动态模板显示

如果需要控件上的按钮:
好的,那么让我们把按钮放在控件上,但是更改内容命令应该在MainWindowViewModel中。要制作这样的绑定,您应该执行以下操作:

Command="{Binding Path=DataContext.ChangeContentCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"

在按钮中,仪表板、房间、屏幕等按钮应位于主窗口上。ViewChangedCommand应位于MainWindowViewModel上。内容将通过动态模板显示

如果需要控件上的按钮:
好的,那么让我们把按钮放在控件上,但是更改内容命令应该在MainWindowViewModel中。要制作这样的绑定,您应该执行以下操作:

Command="{Binding Path=DataContext.ChangeContentCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"

在您的按钮中

我不希望它们出现在主窗口上,我希望能够从每个视图加载控件,并能够在主窗口中使用它们,我不希望主窗口上有任何按钮。带有的按钮只能在从contentcontrolOk加载的视图上,那么让我们将按钮放在控件上,但是更改内容命令应该在MainWindowViewModel中。要创建这样的绑定,您应该这样做,但我认为这可能会有一些冗余代码。主窗口上可能有blogal按钮,控件也可以定义自己的按钮如果在WelcomeViewModel中有ViewChangedCommand,并且应该在MainWindowViewModel中,那么控件中的按钮应该像前面提到的那样具有绑定。现在清楚了吗?如果不是,请告诉我。我写的装订行吗?您是否设置了MainWindow.DataContext=new MainWindowViewModel()?我不希望它们出现在主窗口上我希望能够从每个视图加载控件并在主窗口中使用它们我不希望主窗口上有任何按钮。带有的按钮只能在从contentcontrolOk加载的视图上,那么让我们将按钮放在控件上,但是更改内容命令应该在MainWindowViewModel中。要创建这样的绑定,您应该这样做,但我认为这可能会有一些冗余代码。主窗口上可能有blogal按钮,控件也可以定义自己的按钮如果在WelcomeViewModel中有ViewChangedCommand,并且应该在MainWindowViewModel中,那么控件中的按钮应该像前面提到的那样具有绑定。现在清楚了吗?如果不是,请告诉我。我写的装订行吗?是否设置了MainWindow.DataContext=new MainWindowViewModel()?
class MainWindowViewModel : BaseViewModel
{
    private UserControl _content;

    public UserControl Content
    {
        get { return _content; }
        set
        {
            _content = value;
            OnPropertyChanged("Content");
        }
    }



    public void ViewChanged(object o, EventArgs e)
    {
        switch (o.ToString())
        {
            case "Welcome":
                {
                    var welcome = new WelcomeView();
                   Content = welcome;
                    break;
                }
            case "MeetingRoom":
                {
                    var meetingRoom = new MeetingRoomView();
                    Content= meetingRoom;
                    break;
                }
            case "Dashboard":
                {
                    var dashboard = new DashboardView();
                    Content = dashboard;
                    break;
                }
            case "Screen":
                {
                    var screen = new ScreenView();
                    Content = screen;
                    break;
                }
        }

        MessageBox.Show(o.ToString());
    }

    public MainWindowViewModel()

    {

    }

}
}
Command="{Binding Path=DataContext.ChangeContentCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"