Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF MVVM将窗口标题绑定到属性_C#_Wpf_Mvvm_Binding - Fatal编程技术网

C# WPF MVVM将窗口标题绑定到属性

C# WPF MVVM将窗口标题绑定到属性,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,我的客户端应用程序可以连接到不同的服务器应用程序,因此我想动态地将连接信息添加到窗口标题中。标题绑定到ViewModel的属性,启动应用程序后调用其get,但不再更新,而窗口中的其他控件仍正常工作 以下是XAML: <Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micros

我的客户端应用程序可以连接到不同的服务器应用程序,因此我想动态地将连接信息添加到窗口标题中。标题绑定到
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>
MainWindowViewModel的构造函数

public MainWindowViewModel()
{
    MenuItemViewModel server = new MenuItemViewModel { Text = ServerMenu };
    Menu.Add(server);
    AppTitle = "My application title";
    SetConnectionMenuEntry(false);
    //[.. dynamically build my menu ..]
}
启动应用程序后,将正确显示
标题
。然后我连接到服务器:

private void ConnectToServer()
{
    //[.. connect to server ..]
    if (connected)
    {
        SetConnectionMenuEntry(true);
        ConnectionProperty = " - connected to " + serverProxy.url;
        AppTitle  = appTitle + 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);
            }
        }
    }

知道为什么不更新
标题
,而是更新
标签

您在
网格中有
Windows1ViewModel
。参考资料
,但是您可以从代码中为窗口创建新的数据上下文。您有两个这样的ViewModel实例。

如问题的原始海报所述:


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

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

[...]

您在参考资料中有
Windows1ViewModel
,但是可以从代码中为窗口创建新的DataContext。我们不是有两个这样的ViewModel实例吗?我同意Aphelion,我认为您两次(在代码和XAML中)创建的问题我同意您的看法。您有一个用于窗口的viewmodel和一个专门用于栅格的viewmodel。标题绑定到窗口viewmodel,标签绑定到栅格viewmodel。您只更新第二个菜单(菜单也使用此菜单),因此标题不会更改。解决方案是删除网格的DataContext,因为它继承了窗口的DataContext。@Aphelion:您的注释应该作为答案发布。输出只有两种类型的输出,但两者似乎都未连接到问题:System.Windows.Data错误:23:无法使用默认转换将“en-US”区域性的“”从类型“”转换为类型“System.Windows.Media.ImageSource”;考虑使用转换器的绑定属性。线程“”(0x1c30)已退出,代码为0(0x0)。
<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>