2012-08-15 2 views
0

마스터보기와 두 개의 하위보기가 있습니다. SubViewA에서 버튼을 클릭하면 SubViewA에서 SubViewB로 전환하고 싶습니다. masterview에는 View에 바인드되고로드 될 때 SubViewB로 초기화되는 contentpresenter가 들어 있습니다. SubViewA의 버튼을 클릭하면 SubViewB 생성자가 호출되지만 컨트롤이로드되지 않습니다. 내가 뭘 놓치고 있니? 나는 또한 contenttemplate을 설정하여 시도했다.바운드 개체가 업데이트 될 때 ContentPresenter의 바인딩이 업데이트되지 않습니다.

<ContentPresenter x:Name="contentPresenter" Content="{Binding View, PresentationTraceSources.TraceLevel=High}" /> 

역시 작동하지 않는다.

MainWindow를 :

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:WpfApplication2"> 

<Grid> 

    <TextBlock Text="MasterViewPage" /> 

    <ContentControl x:Name="content" Content="{Binding View}"> 
     <ContentControl.Resources> 
      <DataTemplate DataType="{x:Type local:SubViewModelA}"> 
       <local:SubViewA></local:SubViewA> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type local:SubViewModelB}"> 
       <local:SubViewB></local:SubViewB> 
      </DataTemplate> 
     </ContentControl.Resources> 
    </ContentControl> 

</Grid> 
</Window> 

public partial class MainWindow 
{ 
    public MainWindow() 
    { 
     Loaded += MainWindow_Loaded; 
     InitializeComponent(); 
    } 

    private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     DataContext = new MainViewModel(); 
    }  
} 

SubViewA :

<UserControl x:Class="WpfApplication2.SubViewA" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 

<Grid Margin="0,40,0,0"> 
    <TextBlock Text="Subview A" /> 

    <Button Height="50" Width="120" Content="Open View B" Command="{Binding OpenViewCommand}" /> 
</Grid> 
</UserControl> 

public partial class SubViewA 
{ 
    public SubViewA() 
    { 
     Loaded += SubViewA_Loaded; 
     InitializeComponent(); 
    } 

    private void SubViewA_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     DataContext = new SubViewModelA(); 
    } 
} 

ViewModels : 사전에

public class MainViewModel : NotifyPropertyChanged 
{ 
    private object _view; 

    public object View 
    { 
     get { return _view; } 
     set 
     { 
      _view = value; 
      RaisePropertyChanged(() => View); 
     } 
    } 

    public MainViewModel() 
    { 
     View = new SubViewA(); 
    } 
} 

public class SubViewModelA : MainViewModel 
{ 
    public ICommand OpenViewCommand 
    { 
     get { return new DelegatingCommand(OpenView); } 
    } 

    private void OpenView() 
    { 
     View = new SubViewB(); 
    } 
} 

public class SubViewModelB : MainViewModel 
{ 
} 

감사합니다.

답변

0

OK, 나를 위해 일한 솔루션했다 :

MainView.xaml :

<ContentControl x:Name="content"> 
     <ContentControl.Style> 
      <Style TargetType="{x:Type ContentControl}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding View, PresentationTraceSources.TraceLevel=High}" Value="SubViewA"> 
          <Setter Property="ContentTemplate"> 
          <Setter.Value> 
            <DataTemplate> 
             <local:SubViewA /> 
            </DataTemplate> 
          </Setter.Value> 
         </Setter> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding View, PresentationTraceSources.TraceLevel=High}" Value="SubViewB"> 
          <Setter Property="ContentTemplate"> 
          <Setter.Value> 
            <DataTemplate> 
             <local:SubViewB /> 
            </DataTemplate> 
           </Setter.Value> 
         </Setter> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ContentControl.Style> 
    </ContentControl> 

및 하위보기 A.xaml :

<Button Height="50" Width="120" Content="Open View B" Command="{Binding Path=DataContext.OpenViewCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}, PresentationTraceSources.TraceLevel=High}" /> 

이유 (MainViewModel에서 상속하는) 대신보기가 MainViewModel에 설정되지 않았 음을했지만, 서브 뷰에. MainViewModel에서 실제로 뷰를 설정하기 위해 상속을 제거하면 모든 것이 효과적입니다.

1

보기 - 모델은 그 대신에 enum와 트리거, here is an example (당신이뿐만 아니라 ContentTemplate 대신 Content의를 설정할 수 있습니다) 될 수있는 특성 ViewMode이 뷰에 대한 참조를 포함 할 수 없습니다.

0

모델에서 직접보기를 설정하는 것이 좋지 않다는 데 동의 할 수 있습니다. 하지만 어쨌든, 나는 당신의 솔루션을 (둘 다) 시도하고 SubViewB 여전히로드되지 않습니다. 생성자가 호출되었지만 결코 SubViewB_Loaded입니다. 결과적으로 SubViewB가 표시되지 않습니다.

datacontextchangedcontentcontrol에서 절대로 트리거되지 않습니다. 그럼에도 불구하고 나는 뭔가를 놓치고있다.

주요보기 :

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:WpfApplication2"> 

<Grid> 

    <TextBlock Text="MasterViewPage" /> 

    <ContentControl x:Name="content"> 
     <ContentControl.Style> 
      <Style TargetType="{x:Type ContentControl}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding View}" Value="SubViewA"> 
         <Setter Property="Content"> 
          <Setter.Value> 
           <local:SubViewA /> 
          </Setter.Value> 
         </Setter> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding View}" Value="SubViewB"> 
         <Setter Property="Content"> 
          <Setter.Value> 
           <local:SubViewB /> 
          </Setter.Value> 
         </Setter> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ContentControl.Style> 
    </ContentControl> 
</Grid> 
</Window> 

그리고 뷰 모델에 :

public class MainViewModel : NotifyPropertyChanged 
{ 
    private string _view; 

    public string View 
    { 
     get { return _view; } 
     set 
     { 
      _view = value; 
      RaisePropertyChanged(() => View); 
     } 
    } 

    public MainViewModel() 
    { 
     View = "SubViewA"; 
    } 
} 

public class SubViewModelA : MainViewModel 
{ 
    public ICommand OpenViewCommand { get { return new DelegatingCommand(OpenView); } } 

    private void OpenView() 
    { 
     View = "SubViewB"; 
    } 
} 

public class SubViewModelB : MainViewModel 
{ 
} 
관련 문제