WPF How Can I Modify ContentControl Content

How can I change content management content when I click a button. For example, I have content management using a user control named "Homepage", when I clicked on a button, I want to change the content of the content using 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 have little time.

+3
source share
2 answers

You basically need one main or parent model. This view model must have a type property BaseViewModelin it, for example, with a name ViewModel. All your other pageview models should expand this base class.

<ContentControl Content="{Binding ViewModel}" />

, BaseViewModel, ViewModel... :

ViewModel = new HomeView();

? DataTemplate ... , :

<DataTemplate DataType="{x:Type ViewModels:HomeViewModel}">
    <Views:HomeView />
</DataTemplate>

WPF , ViewModel. , , Button? ICommand , Button , RelayCommand. , :

<Grid>
    <!-- Add your Buttons or Menu here, above where the views will be -->
    <ContentControl Content="{Binding HomePage}"/>
</Grid>

, a Button, ViewModel, DataTemplate , . ICommand, RelayCommand, , :

public ICommand DisplayHomeView
{
    get { return new ActionCommand(action => ViewModel = new HomeViewModel(), 
        canExecute => !IsViewModelOfType<HomeViewModel>()); }
}
+7

, , , . , -:-)....

, : 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>
0

All Articles