c# - WPF-MVVM-将窗口标题绑定(bind)到属性

标签 c# wpf mvvm binding

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

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

Title 绑定(bind)到 AppTitle,而 Label 绑定(bind)到 ConnectionProperty,工作正常。在 XAML.cs 中,我将 ViewModel 设置为 ViewDataContext:

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 没有更新吗?

最佳答案

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

关于c# - WPF-MVVM-将窗口标题绑定(bind)到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397728/

相关文章:

c# - 如何在代码隐藏中编辑数据网格中的单个单元格

wpf - M-V-VM WPF : Way to maintain Databinding while passing parent object of Databound object into IValueConverter?

使用 MVVMCROSS 的 WPF 应用程序

C# 处理无效的用户输入

c# - 正则表达式检测单词是否是字符串的一部分并且下一个单词是否未大写

c# - WPF 用户控件中的数据绑定(bind)

c# - 寻找在 WPF 中显示数据行的最简单方法

wpf - 使用 .NET 5 的独立部署模式创建 Visual Studio 安装项目

c# - 无法解析此引用。找不到程序集

c# - 使用 Selenium 保存页面中的图像