0

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

主窗口视图:

<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>

主窗口视图模型:

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....";
    }

}

更改显示视图模型:

public class changeDisplayViewModel: PropertyChangedBase
{
}

更改显示视图:

<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”被更新并显示,但用户控件没有。我究竟做错了什么?

4

1 回答 1

1

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

于 2012-06-10T22:02:46.340 回答