WPF 如何更改 ContentControl 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21689811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF How can i change ContentControl content
提问by Диян Богданов
How can i change content control's content when button is clicked. For example i have content control with user control named "Home Page" when i clicked at some button i wanna change content control' content with another user control.
单击按钮时如何更改内容控件的内容。例如,当我点击某个按钮时,我有一个名为“主页”的用户控件的内容控件,我想用另一个用户控件更改内容控件的内容。
<Window ...
xmlns:ViewModel="clr-namespace:PAL_PlayAndLearn.ViewModels"
xmlns:Pages="clr-namespace:PAL_PlayAndLearn.Pages"
...>
<Window.DataContext>
<ViewModel:AppViewModel />
</Window.DataContext>
<Grid>
<ContentControl Content="{Binding HomePage}"/>
</Grid>
</Window>
Can you help me, because i don't have many time.
你能帮我吗,因为我没有很多时间。
回答by Sheridan
You basically need one main, or parent view model. This view model should have a property of type BaseViewModelin it, let's say named ViewModel. Allof your other page view models should extend this base class.
您基本上需要一个主视图模型或父视图模型。这个视图模型应该有一个 type 属性,BaseViewModel比如 named ViewModel。所有其他页面视图模型都应该扩展这个基础class。
<ContentControl Content="{Binding ViewModel}" />
Now, because your view models all have a base type of BaseViewModel, they can allbe set as the value for the ViewModelproperty... so to load a new view model, you set it as the value for this property:
现在,因为你的视图模型都有一个基本类型BaseViewModel,它们都可以被设置为ViewModel属性的值......所以要加载一个新的视图模型,你将它设置为这个属性的值:
ViewModel = new HomeView();
But how do we display the related views? Let's use DataTemplates to do that for us... add this and a similar entry for every view model that you will be using here:
但是我们如何显示相关的视图呢?让我们使用DataTemplates 来为我们做这件事...为您将在此处使用的每个视图模型添加这个和一个类似的条目:
<DataTemplate DataType="{x:Type ViewModels:HomeViewModel}">
<Views:HomeView />
</DataTemplate>
Now WPF will render our views whenever we set the relating view model as the value for the ViewModelproperty. So finally, the Button? Well for that we need an ICommandin the main view model that is hooked to a Buttonin the main view and I recommend using the RelayCommand. Note that you'll need to alter your main view:
现在,只要我们将相关视图模型设置为ViewModel属性值,WPF 就会呈现我们的视图。所以最后,Button? 好吧,为此我们需要ICommand在主视图模型中连接到Button主视图中的 a,我建议使用RelayCommand. 请注意,您需要更改主视图:
<Grid>
<!-- Add your Buttons or Menu here, above where the views will be -->
<ContentControl Content="{Binding HomePage}"/>
</Grid>
So, when a Buttonis clicked in the main view, you simply change the value of the ViewModelproperty and the DataTemplatewill ensure that the relevant view is rendered in the UI. This is one of my own custom ICommandimplementations like the RelayCommandthat would load the view model if it wasn't already loaded:
因此,当Button在主视图中单击a 时,您只需更改ViewModel属性的值即可DataTemplate确保相关视图在 UI 中呈现。这是我自己的自定义ICommand实现之一,例如RelayCommand如果尚未加载将加载视图模型:
public ICommand DisplayHomeView
{
get { return new ActionCommand(action => ViewModel = new HomeViewModel(),
canExecute => !IsViewModelOfType<HomeViewModel>()); }
}
回答by Eric Ouellet
Sheridan answer is excellent but I give a full solution because I loose some time to put everything together. Hope it could help someone :-) ....
Sheridan 的回答非常好,但我给出了一个完整的解决方案,因为我浪费了一些时间将所有内容放在一起。希望它可以帮助某人:-) ....
Please note than local:UserControlSpecialSignalTtrModel inherits from SignalProviderSpecial.
请注意 local:UserControlSpecialSignalTtrModel 继承自 SignalProviderSpecial。
<UserControl
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:local="clr-namespace:ParametricStudyAnalysis.ScopeSelection.Special"
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" x:Class="ParametricStudyAnalysis.ScopeSelection.Special.UserControlAddSpecialSignal"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<local:UserControlAddSpecialSignalModel></local:UserControlAddSpecialSignalModel>
</UserControl.DataContext>
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:UserControlSpecialSignalTtrModel}">
<local:UserControlSpecialSignalTtr/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Header="Signal type" Grid.Row="0" Padding="5">
<xcdg:DataGridControl Name="DataGrid" SelectionMode="Single" ItemsSource="{Binding SpecialSignalEntries}"
SelectedItem="{Binding SpecialSignalEntrySelected}" Height="200">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="Name" Title="Type of special signal" ReadOnly="True"></xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</GroupBox>
<GroupBox Header="Parameters" Grid.Row="1" Margin="0,3,0,0" Padding="5">
<ContentControl Name="MyContentControl"
DataContext="{Binding SpecialSignalEntrySelected, Mode=OneWay}"
Content="{Binding SignalProviderSpecial}">
</ContentControl>
</GroupBox>
</Grid>
</UserControl>

