2017-01-26 5 views
0

コンボボックスでユーザーが選択した内容に基づいてウィンドウのレイアウトを変更する必要があります。私は片方の方法で刺すようにしましたが、それは頑丈で、ハッキングされているように感じます。 Imは、きれいなMVVMソリューションでなければならないと確信しています。WPF MVVMを使用したコンボボックス選択によるウィンドウレイアウトの変更

私のグループボックスに複数のドッキングパネルを配置する場所を考えています。可視性が崩壊するように設定されています。選択が行われると、適切なドックパネルが表示可能に設定されます。私はビューモデル内でこれを成功させる方法を見つけようとしました。私はまた、私の試みがMVVMに違反していると考えているが、考えていない。

XAML

<GroupBox Header="Options"> 
    <Grid> 
     <DockPanel LastChildFill="False" x:Name="syncWellHeadersDockPanel" Visibility="Collapsed"> 
      <Button DockPanel.Dock="Right" Content="Test"></Button> 
     </DockPanel> 
     <DockPanel LastChildFill="False" x:Name="SyncDirectionalSurveyDockPanel" Visibility="Collapsed"> 
      <Button DockPanel.Dock="Left" Content="Test02"></Button> 
     </DockPanel> 

    </Grid> 
</GroupBox> 

のViewModel - コンボボックスのために選択した項目のプロパティ

private StoredActionsModel _selectedStoredAction = DefaultStoredAction.ToList<StoredActionsModel>()[0]; 
     public StoredActionsModel SelectedStoredAction 
     { 
      get { return _selectedStoredAction; } 
      set 
      { 
       if (value != _selectedStoredAction) 
       { 
        // Unset Selected on old value, if there was one 
        if (_selectedStoredAction != null) 
        { 
         _selectedStoredAction.Selected = false; 
        } 
        _selectedStoredAction = value; 
        // Set Selected on new value, if there is one 
        if (_selectedStoredAction != null) 
        { 
         _selectedStoredAction.Selected = true; 
        } 
        OnPropertyChanged("SelectedStoredAction"); 

        if (_selectedStoredAction.StoredActionID == 4) 
        { 
         //X:SyncWellHeaderDockPanel.visibility = true????? 
        } 
       } 
      } 
     } 
+0

可視性コンバータにはboolが必要です - https://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx、その他のSOの質問もありますboolを可視性に変換します。 – Dhanashree

答えて

3

ここであなたがする方法を求めている正確に何を行うには純粋なXAML方法です。これは少し冗長です。

Visibilityは、DockPanelの属性に設定されていないことに注意してください。それでもやっていれば、Styleトリガーで設定された値は属性によって上書きされます。これが依存関係のプロパティの仕組みです。

<GroupBox Header="Options"> 
    <Grid> 
     <DockPanel LastChildFill="False" x:Name="syncWellHeadersDockPanel" > 
      <Button DockPanel.Dock="Right" Content="Test"></Button> 
      <DockPanel.Style> 
       <Style TargetType="DockPanel" > 
        <Setter Property="Visibility" Value="Collapsed" /> 
        <Style.Triggers> 
         <DataTrigger 
          Binding="{Binding SelectedStoredAction.StoredActionID}" 
          Value="1" 
          > 
          <Setter Property="Visibility" Value="Visible" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DockPanel.Style> 
     </DockPanel> 
     <DockPanel LastChildFill="False" x:Name="SyncDirectionalSurveyDockPanel"> 
      <Button DockPanel.Dock="Left" Content="Test02"></Button> 
      <DockPanel.Style> 
       <Style TargetType="DockPanel" > 
        <Setter Property="Visibility" Value="Collapsed" /> 
        <Style.Triggers> 
         <DataTrigger 
          Binding="{Binding SelectedStoredAction.StoredActionID}" 
          Value="2" 
          > 
          <Setter Property="Visibility" Value="Visible" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DockPanel.Style> 
     </DockPanel> 
    </Grid> 
</GroupBox> 

DataTemplateSelectorSelectedStoredAction.StoredActionIDを渡すことになり、これを行うための別の方法、それはあなたのXAMLリソースキーが何であるかを知っているC#のコードを書く必要、と私はファンではありません。

+2

最近、私はあなたのために何かが難しくなるでしょう! – LCaraway

関連する問題