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>