Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Canvas_User Controls - Fatal编程技术网

在WPF的画布上添加/删除用户控件

在WPF的画布上添加/删除用户控件,wpf,canvas,user-controls,Wpf,Canvas,User Controls,我创建了两个WPF用户控件,它们有多个文本框、组合框和按钮。在主wpf窗口中,我创建了一个画布和一个列表框,其中包含两个listitems。我的要求是,如果选择第一个listbox项,那么第一个用户控件应该添加到画布中。如果选择第二个listbox项,则先前添加的用户控件应该隐藏,第二个控件应该添加/显示。有人能为这段代码提供一个示例吗 下面是我编写的代码。我创建了两个用户控件 UserControl1.xaml <UserControl x:Class="UserControl1

我创建了两个WPF用户控件,它们有多个文本框、组合框和按钮。在主wpf窗口中,我创建了一个画布和一个列表框,其中包含两个listitems。我的要求是,如果选择第一个listbox项,那么第一个用户控件应该添加到画布中。如果选择第二个listbox项,则先前添加的用户控件应该隐藏,第二个控件应该添加/显示。有人能为这段代码提供一个示例吗

下面是我编写的代码。我创建了两个用户控件

UserControl1.xaml

    <UserControl x:Class="UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="210" Height="210" x:Name="UCntl1">
    <Grid>
        <StackPanel>
        <GroupBox Header="Text Boxes">
            <StackPanel Orientation="Horizontal">
                <StackPanel>
                    <Label Margin="4" Height="21">TextBox1</Label>
                    <Label Margin="4" Height="21">TextBox2</Label>
                    <Label Margin="4" Height="21">TextBox3</Label>
                </StackPanel>
                <StackPanel>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.1</TextBox>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.2</TextBox>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.3</TextBox>
                </StackPanel>
            </StackPanel>
        </GroupBox>
            <GroupBox Header="Conbo Boxes">
                <StackPanel Orientation="Horizontal">
                    <StackPanel>
                        <Label Margin="4" Height="21">ComboBox1</Label>
                        <Label Margin="4" Height="21">ComboBox2</Label>
                        <Label Margin="4" Height="21">ComboBox3</Label>
                    </StackPanel>
                    <StackPanel>
                        <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
                            <ComboBoxItem>Item AAA</ComboBoxItem>
                        </ComboBox>
                        <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
                            <ComboBoxItem>Item BBB</ComboBoxItem>
                        </ComboBox>
                        <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
                            <ComboBoxItem>Item CCC</ComboBoxItem>
                        </ComboBox>
                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </Grid>
</UserControl>
<UserControl x:Class="UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="210" Height="210" x:Name="UCntl2">
<Grid>
    <StackPanel>
        <GroupBox Header="Conbo Boxes">
            <StackPanel Orientation="Horizontal">
                <StackPanel>
                    <Label Margin="4" Height="21">ComboBox1</Label>
                    <Label Margin="4" Height="21">ComboBox2</Label>
                    <Label Margin="4" Height="21">ComboBox3</Label>
                </StackPanel>
                <StackPanel>
                    <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
                    <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
                    <ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
                </StackPanel>
            </StackPanel>
        </GroupBox>
        <GroupBox Header="Text Boxes">
            <StackPanel Orientation="Horizontal">
                <StackPanel>
                    <Label Margin="4" Height="21">TextBox1</Label>
                    <Label Margin="4" Height="21">TextBox2</Label>
                    <Label Margin="4" Height="21">TextBox3</Label>
                </StackPanel>
                <StackPanel>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
                    <TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
                </StackPanel>
            </StackPanel>
        </GroupBox>
    </StackPanel>
</Grid>
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="Auto" Width="Auto">
<Grid>
    <StackPanel Orientation="Horizontal">
        <Grid Margin="0,0,0,0" Width="Auto" Height="Auto" VerticalAlignment="Top">
            <ListBox Margin="0,0,0,0" HorizontalAlignment="Left" Width="Auto" Height="Auto" VerticalAlignment="Top" BorderBrush="White">
                <ListBoxItem Name="LstItem1" Selected="LstItem1_Selected">User Control 1</ListBoxItem>
                <ListBoxItem Name="LstItem2" Selected="LstItem2_Selected">User Control 2</ListBoxItem>
            </ListBox>
        </Grid>
        <Grid Width="10" Background="LightGray"></Grid>
        <Grid Margin="0,0,0,0">
            <Canvas Name="Canvas1" Width="210" Height="210" VerticalAlignment="Top">

            </Canvas>
        </Grid>

    </StackPanel>
</Grid>
末级

问题


在删除画布中以前的控件后,我添加了用户控件。因此,在将控件重新添加到画布时,UserControls列表框中的选定值将被清除。是否有任何方法可以在画布上隐藏用户控件,而不是从画布上删除控件。

这应该可以让您开始。这不完全是我在真实项目中的做法,但已经足够接近了。我希望有一个简单的方式来附加项目的答案在这个网站上

最终结果如下所示:

App.xaml

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
ViewModel.cs

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
ContentViewModel.cs

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
FirstContentViewModel.cs

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
SecondContentViewModel.cs

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
MainViewModel.cs

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>
namespace SO9735486.ViewModels
{
使用System.Collections.Generic;
使用System.Linq;
公共密封类MainViewModel:ViewModel
{
私有只读ICollection contentViewModels;
私有ContentViewModel选择ContentViewModel;
公共主视图模型()
{
this.contentViewModels=新列表
{
新的FirstContentViewModel(),
新的SecondContentViewModel()
};
this.selectedContentViewModel=this.contentViewModels.First();
}
公共ICollection内容视图模型
{
获取{返回this.contentViewModels;}
}
公共内容视图模型已选择内容视图模型
{
获取{返回this.selectedContentViewModel;}
设置
{
if(this.selectedContentViewModel!=值)
{
this.selectedContentViewModel=值;
此.OnPropertyChanged(“SelectedContentViewModel”);
}
}
}
}
}
FirstContentView.xaml

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>

第一内容视图
SecondContentView.xaml

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>

第二内容视图
MainView.xaml

<Application x:Class="SO9735486.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SO9735486.ViewModels"
             xmlns:v="clr-namespace:SO9735486.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewModel}">
            <v:MainView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:FirstContentViewModel}">
            <v:FirstContentView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondContentViewModel}">
            <v:SecondContentView/>
        </DataTemplate>
    </Application.Resources>
</Application>
<Window x:Class="SO9735486.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
</Window>
namespace SO9735486
{
    using System.Windows;
    using SO9735486.ViewModels;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Content = new MainViewModel();
        }
    }
}
namespace SO9735486.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Threading;

    // http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;

        protected ViewModel()
        {
            if (Application.Current != null)
            {
                _dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //this is useful for unit tests where there is no application running
                _dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace SO9735486.ViewModels
{
    public abstract class ContentViewModel : ViewModel
    {
        private readonly string displayName;

        protected ContentViewModel(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return this.displayName; }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class FirstContentViewModel : ContentViewModel
    {
        private string name;

        public FirstContentViewModel()
            : base("First")
        {
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
    }
}
namespace SO9735486.ViewModels
{
    public sealed class SecondContentViewModel : ContentViewModel
    {
        public SecondContentViewModel()
            : base("Second")
        {
        }
    }
}
namespace SO9735486.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class MainViewModel : ViewModel
    {
        private readonly ICollection<ContentViewModel> contentViewModels;
        private ContentViewModel selectedContentViewModel;

        public MainViewModel()
        {
            this.contentViewModels = new List<ContentViewModel>
            {
                new FirstContentViewModel(),
                new SecondContentViewModel()
            };

            this.selectedContentViewModel = this.contentViewModels.First();
        }

        public ICollection<ContentViewModel> ContentViewModels
        {
            get { return this.contentViewModels; }
        }

        public ContentViewModel SelectedContentViewModel
        {
            get { return this.selectedContentViewModel; }
            set
            {
                if (this.selectedContentViewModel != value)
                {
                    this.selectedContentViewModel = value;
                    this.OnPropertyChanged("SelectedContentViewModel");
                }
            }
        }
    }
}
<UserControl x:Class="SO9735486.Views.FirstContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>First Content View</Label>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.SecondContentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Label>Second content view</Label>
        <Rectangle Fill="Blue" Width="100" Height="30"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="SO9735486.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding ContentViewModels}" SelectedItem="{Binding SelectedContentViewModel}" DisplayMemberPath="DisplayName"/>
        <ContentControl Content="{Binding SelectedContentViewModel}"/>
    </DockPanel>
</UserControl>

仅对Windows1.xaml.vb进行更改。代码bhind文件应如下所示。问题的答案是,将添加的用户控件的可见性设置为Windows.Visibility.Collapsed,而不是callinf Canvas1.Clear()

Window1.xaml.vb

Class Window1 

Private Sub LstItem1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Canvas1.Children.Clear()
    Canvas1.Children.Add(New UserControl1)
End Sub

Private Sub LstItem2_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Canvas1.Children.Clear()
    Canvas1.Children.Add(New UserControl2)
End Sub
Class Window1 
Private U1 As Integer
Private U2 As Integer
Private Sub LstItem1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Canvas1.Children.Item(U1).Visibility = Windows.Visibility.Visible
    Canvas1.Children.Item(U2).Visibility = Windows.Visibility.Collapsed
End Sub

Private Sub LstItem2_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Canvas1.Children.Item(U2).Visibility = Windows.Visibility.Visible
    Canvas1.Children.Item(U1).Visibility = Windows.Visibility.Collapsed
End Sub

Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    U1 = Canvas1.Children.Add(New UserControl1)
    U2 = Canvas1.Children.Add(New UserControl2)

    Canvas1.Children.Item(U1).Visibility = Windows.Visibility.Collapsed
    Canvas1.Children.Item(U2).Visibility = Windows.Visibility.Collapsed
End Sub
End Class

你能在我的问题中看到上面贴出的代码吗?如果你对这个boogaart有任何解决方案,请回答我。谢谢你的关注。我得到了答案。我不应该从画布(CanvasForm.Clear)中删除控件,而应该将控件的可见性设置为折叠。我已经发布了这个答案。由于我是这个领域的新手(WinForms和WPF),我不知道Visibility属性。再次感谢您抽出时间。