WPF-MVVM-绑定属性的窗口标题

时间:2013-01-18 11:12:55

标签: c# wpf mvvm binding

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

以下是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绑定到AppTitle,而Label绑定到ConnectionProperty,这工作正常。在XAML.cs我将ViewModel设置为DataContext的{​​{1}}:

View

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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>