2013-12-19 25 views
0

這將很難解釋,但忍受着我。需要一個切換菜單,當它失去焦點時關閉

我使用MVVM模式在WPF中創建應用程序。我是新手,這就是爲什麼我問這個問題。

我有應用程序設置爲3個頁面或窗口內的意見。其中一個是靜態的,總是存在,另外兩個是一些小設置頁面,它們使用zindex在頂部的頂部打開。此時打開這些頁面的菜單使用帶有togglebuttons的列表框作爲模板(選中的狀態綁定到列表框),以便您可以單擊以打開菜單,然後再次單擊該按鈕關閉它。

在理想的世界中,我喜歡它,所以如果菜單頁失去焦點(聽一下靜態視圖的點擊?),設置視圖也會關閉。此外,我想知道是否有人有一個簡單的解決方案,以類似的方式工作的菜單,因爲目前這是一個相當混亂的解決方案。下面是一些代碼示例:

<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding PageViewModels}" SelectedItem="{Binding CurrentPageViewModel}"> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <TextBlock Margin="10,0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FCCC"/> 
        <ToggleButton 
          VerticalAlignment="Stretch" 
          Content="" 
          IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" 
          /> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

    <!-- Settings views --> 
    <ContentControl Panel.ZIndex="2" Grid.Row="1" Grid.Column="0" Content="{Binding CurrentPageViewModel}"/> 

    <!-- Main page view --> 
    <ContentControl Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Width="1000" Height="700" Content="{Binding StaticPageViewModel}"/> 

我使用這個blog post的概念來管理我的看法和的ViewModels,但我改變了菜單顯示,所以我可以刪除一個改變網頁的需要的方式命令/ ICommand的。

TL; DR:我正在尋找建議和批評意見,以改進我目前創建菜單欄的方式。

+0

定義關閉,是指當它失去焦點時隱藏切換菜單? –

+0

不,我的意思是關閉他們代表的觀點,對不起,我沒有說清楚。目前基本上,雙向綁定使'CurrentPageViewModel'屬性爲null,因此視圖被關閉。 – pgwri

+0

所以你想要一個關閉「視圖」的切換按鈕? –

回答

0

我會創建一個附加的屬性,如果你真的想要MVVM風格來關閉視圖,當它失去焦點。

public static readonly DependencyProperty CloseViewOnLostFocusProperty = 
     DependencyProperty.RegisterAttached("CloseViewOnLostFocus", typeof (object), typeof (MainWindow), new FrameworkPropertyMetadata(default(object), RegisterLostFocus)); 

    private static void RegisterLostFocus(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
    { 
     //This is the control that is attached to it e.g. ContentControl 
     var sender = dependencyObject as FrameworkElement; 

     //Your ViewModel 
     var viewModel = dependencyPropertyChangedEventArgs.NewValue as ViewModel; 
     if (sender != null) 
     { 
      sender.LostFocus += (o, args) => 
       { 
        //Close whatever you are doing right now to close the View. 
        viewModel.Close(); 
       }; 
     } 
    } 

並且在您的視圖中,您可以附加任何您想關閉的ViewModel,當它失去焦點時,例如,你的SettingsView獲得了LostFocus,它會關閉這個視圖。在這裏,我在我的MainWindow類上創建了一個附加屬性。

<!-- Settings views --> 
    <ContentControl 
MainWindow.CloseViewOnLostFocus="{Binding RelativeSource={RelativeSource Self},Path=Content}" 
x:Name="SettingsView" Panel.ZIndex="2" Grid.Row="1" Grid.Column="0" Content="{Binding CurrentPageViewModel}"/> 

    <!-- Main page view --> 
    <ContentControl x:Name="MainPageView" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Width="1000" Height="700" Content="{Binding StaticPageViewModel}"/> 
0

您可以將ContextMenu附加到按鈕上,該按鈕在單擊按鈕時打開。這樣,未聚焦時的關閉行爲完全是自動的。

然後,您可以重新調整菜單,然後根據需要進行調整。

相關問題