将用户控件动态添加到Caliburm Micro [英] Add a usercontrol to caliburm micro dynamically

查看:70
本文介绍了将用户控件动态添加到Caliburm Micro的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Caliburn Micro与WPF一起使用。我想创建一个应用程序,该应用程序的左侧是菜单,右侧是网格。单击菜单项时,右侧的网格将更改为另一个视图。另一个视图将在单独的文件中。

I am using Caliburn Micro with WPF. I want to create an application with a menu on the left side, and a grid on the right side of the application. When clicking on a menu item, the grid on the right side, will change to another view. The another view will be in a separate file.

MainWindowView:

MainWindowView:

<UserControl x:Class="CMDemo.Views.MainWindowView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="90*" />
        <ColumnDefinition Width="210*" />
    </Grid.ColumnDefinitions>
    <StackPanel Name="LeftMenu">
        <Button Name="ChangeDisplay" Content="Click Me"></Button>
        <TextBlock x:Name="MyString"></TextBlock>
    </StackPanel>
    <Grid Grid.Column="1" x:Name="MainGridContent" />
</Grid>

MainWindowViewModel:

MainWindowViewModel:

public class MainWindowViewModel : PropertyChangedBase
{

    private UserControl mainGridContent;
    private string myString;

    public UserControl MainGridContent 
    {
        get { return this.mainGridContent; }
        set
        {
            this.mainGridContent = value;
            NotifyOfPropertyChange(() => this.MainGridContent);
        }   
    }

    public string MyString
    {
        get { return this.myString; }
        set
        {
            this.myString = value;
            NotifyOfPropertyChange(() => this.MyString);
        }
    }

    public void ChangeDisplay()
    {
        this.MainGridContent = new ChangeDisplayView();
        this.MyString = "Testing....";
    }

}

changeDisplayViewModel:

The changeDisplayViewModel:

public class changeDisplayViewModel: PropertyChangedBase
{
}

changeDisplayView:

The changeDisplayView:

<UserControl x:Class="CMDemo.Views.changeDisplayView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox content="Hello Caliburn Micro">
</Grid>

当我单击单击我按钮,TextBlock MyString已更新并显示,但用户控件未更新。我在做什么错?

When I click the "Click Me" button the TextBlock "MyString" is updated and showing, but the usercontrol is not. What am I doing wrong?

推荐答案

尝试将MainGridContent更改为changeDisplayViewModel而不是视图本身。

Try changing MainGridContent to a changeDisplayViewModel rather than the view itself.

这篇关于将用户控件动态添加到Caliburm Micro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆