2013-01-18 2 views
2

클라이언트 응용 프로그램이 다른 서버 응용 프로그램에 연결할 수 있으므로 연결 정보를 동적으로 창 제목에 추가하고 싶습니다. 제목은 ViewModel의 속성에 바인딩되며 앱을 시작한 후에는 get이 호출되지만 더 이상 업데이트되지 않는 반면 윈도우의 다른 컨트롤은 여전히 ​​올바르게 작동합니다. 여기 속성에 대한 WPF-MVVM- 바인드 창 제목

XAML이다 미세 Label ConnectionProperty 작동하고, 결합하는 동안

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

TitleAppTitle에 결합된다.

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 올바르게 표시되는 XAML.cs에서 I는 상기 ViewDataContext-ViewModel 세트. 그럼 서버에 연결하십시오 LabelConnectionProperty 값을 가져옵니다 동안이 후

private void ConnectToServer() 
{ 
    //[.. connect to server ..] 
    if (connected) 
    { 
     SetConnectionMenuEntry(true); 
     ConnectionProperty = " - connected to " + serverProxy.url; 
     AppTitle = appTitle + connectionProperty; 
    } 
} 

에서, Title은 동일합니다. 여기

은 거의 동일하다, 두 속성의 정의입니다 :

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?

+3

을 당신은'Windows1ViewModel'이 그러나 리소스에서 코드에서 창에 대한 새로운 DataContext를 만듭니다. 이런 식으로 ViewModel의 인스턴스가 두 개 없습니까? – Aphelion

+0

Aphelion에 동의합니다. 코드에 두 번 (XAML에서) 생성하는 문제가 있다고 생각합니다. – chameleon86

+0

나는 당신에게 동의합니다. 창에 대한 하나의보기 모델과 특별히 그리드에 대한 하나의보기 모델이 있습니다. 제목은 window-viewmodel에 바인딩되고 레이블은 grid-viewmodel에 바인딩됩니다. 두 번째 메뉴 (메뉴에서도 사용) 만 업데이트되므로 제목이 변경되지 않습니다. 해결 방법은 창의 DataContext를 상속하므로 Grid의 DataContext를 제거하는 것입니다. – webber2k6

답변

1

Grid.ResourcesWindows1ViewModel이 있지만 코드에서 창에 대한 새 DataContext를 만듭니다. 이 방법으로 ViewModel 인스턴스가 두 개 있습니다. 질문의 원래 포스터에 의해 언급 한 바와 같이

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>