添加一个用户控件来caliburm微动态(Add a usercontrol to caliburm

2019-10-31 19:03发布

我使用微卡利与WPF。 我想创建一个在左侧的菜单,并在应用程序的右侧网格的应用程序。 当在菜单项单击,在右边的网格,将切换到其他视图。 的另一视图将是在一个单独的文件中。

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:

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:

public class changeDisplayViewModel: PropertyChangedBase
{
}

该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的”被更新和显示,但是用户控件却不是。 我究竟做错了什么?

Answer 1:

试着改变MainGridContent到changeDisplayViewModel而不是视图本身。



文章来源: Add a usercontrol to caliburm micro dynamically