クライアント アプリケーションはさまざまなサーバー アプリケーションに接続できるため、接続情報をウィンドウ タイトルに動的に追加したいと考えています。タイトルは のプロパティにバインドされており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
にバインドされていますがAppTitle
、Label
は にバインドされてConnectionProperty
おり、正常に動作しています。で、 を のXAML.cs
に設定ViewModel
しDataContext
ます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
が更新されていない理由は何か考えはありLabel
ますか?