2

我的客户端应用程序可以连接到不同的服务器应用程序,因此我想将连接信息动态添加到窗口标题。标题绑定到 的属性,ViewModelget在启动应用程序后调用它,但它不再更新,而窗口中的其他控件仍然正常工作。

这是XAML

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:localVM="clr-namespace:MyApp.ViewModels"
    xmlns:local="clr-namespace:MyApp"
    WindowStartupLocation="CenterScreen"
    Title="{Binding Path=AppTitle}"
    Height="459"
    Width="810">
<Window.Resources>
    [...]
    <localVM:MainWindowViewModel x:Key="Windows1ViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource Windows1ViewModel}">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Canvas Grid.Row="0">
        <Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=Menu}"/>
        <Label Content="{Binding Path=ConnectionProperty}" Canvas.Right="0" Canvas.Bottom="0"/>
    </Canvas>
 </Grid>
</Window>

Titleis bound to AppTitle, while Labelis bound to ConnectionProperty,这工作正常。在XAML.cs我将 设置ViewModelDataContextView

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel();
}

的构造函数MainWindowViewModel

public MainWindowViewModel()
{
    MenuItemViewModel server = new MenuItemViewModel { Text = ServerMenu };
    Menu.Add(server);
    AppTitle = "My application title";
    SetConnectionMenuEntry(false);
    //[.. dynamically build my menu ..]
}

启动应用程序后,Title正确显示。然后我连接到服务器:

private void ConnectToServer()
{
    //[.. connect to server ..]
    if (connected)
    {
        SetConnectionMenuEntry(true);
        ConnectionProperty = " - connected to " + serverProxy.url;
        AppTitle  = appTitle + connectionProperty;
    }
}

在此之后,Title保持不变,而Label获得ConnectionProperty价值。

这是两个属性的定义,几乎相同:

    private string appTitle;
    public string AppTitle 
    {
        get { return appTitle; }
        set 
        {
            if (this.appTitle != value)
            {
                this.appTitle = value;
                RaisePropertyChanged(() => AppTitle);
            }
        }
    }

    private string connectionProperty = "";
    public string ConnectionProperty
    {
        get { return this.connectionProperty; }
        set
        {
            if (this.connectionProperty != value)
            {
                this.connectionProperty = value;
                RaisePropertyChanged(() => ConnectionProperty);
            }
        }
    }

知道为什么Title不更新,但是Label

4

2 回答 2

1

Windows1ViewModel在 中Grid.Resources,但是您从代码中为窗口创建了一个新的 DataContext。通过这种方式,您有两个 ViewModel 实例。

于 2013-01-18T12:11:33.817 回答
0

正如问题的原始海报所述:


我注释掉了我需要删除以使其正常工作的行:

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:localVM="clr-namespace:MyApp.ViewModels"
    xmlns:local="clr-namespace:MyApp"
    WindowStartupLocation="CenterScreen"
    Title="{Binding Path=AppTitle}"
    Height="459"
    Width="810">
<Window.Resources>
    [...]
    <!-- <localVM:MainWindowViewModel x:Key="Windows1ViewModel" /> [Edit: 2 times set] -->
</Window.Resources>
<Grid> <!-- Edit removed: DataContext="{StaticResource Windows1ViewModel}" -->
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Canvas Grid.Row="0">
        <Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=Menu}"/>
        <Label Content="{Binding Path=ConnectionProperty}" Canvas.Right="0" Canvas.Bottom="0"/>
    </Canvas>
 </Grid>
</Window>
于 2013-04-05T17:51:19.370 回答