2013-01-02 591 views
2

我有一個碼頭上的telerik關閉按鈕,我想重寫樣式以允許用戶選擇他們想要關閉的選項。覆蓋Telerik關閉按鈕

以記事本+ +爲例..有一個「關閉」和「關閉所有,但這個」選項。

這正是我想要做的這個telerik radDock關閉按鈕。

我已經研究過這個,找不到任何有用的東西讓我真正開始。我剛開始使用WPF(以及真正的C#),所以任何有用的建議,代碼或示例項目將不勝感激。先進的謝謝你。

Metro Smurf,它與這個教程非常相似。我對WPF和C#很新,所以請好好哈哈。

這是我的XAML:

<Window x:Class="RadDockCloseButton1.MainWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
       xmlns:local="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking" 
       Title="MainWindow" Height="350" Width="525"> 

<Window.Resources> 
    <DataTemplate x:Key="ContextMenuTemplate"> 
     <telerik:RadContextMenu InheritDataContext="False"> 
      <telerik:RadMenuItem 
       IsChecked="{Binding IsFloatingOnly}" 
       Command="telerik:RadDockingCommands.Floating" 
       CommandParameter="{Binding}" 
       CommandTarget="{Binding}" 
       Header="{Binding Command.Text, RelativeSource={RelativeSource Self}}"/> 

      <telerik:RadMenuItem 
       IsChecked="{Binding IsDockableOptionChecked}" 
       Command="telerik:RadDockingCommands.Dockable" 
       CommandParameter="{Binding}" 
       CommandTarget="{Binding}" 
       Header="{Binding Command.Text, RelativeSource={RelativeSource Self}}" /> 

      <telerik:RadMenuItem 
       Command="local:RadDockingCommands.CloseAllButThisCommand" 
       CommandParameter="{Binding}" 
       CommandTarget="{Binding}" 
       Header="{Binding Command.Text, RelativeSource={RelativeSource Self}}" /> 

     </telerik:RadContextMenu> 
    </DataTemplate> 

    <Style TargetType="telerik:RadPane"> 
     <Setter Property="ContextMenuTemplate" Value="{StaticResource ContextMenuTemplate}" /> 
    </Style> 

</Window.Resources> 

<Grid> 
    <telerik:RadDocking x:Name="radDocking"> 
     <telerik:RadDocking.DocumentHost> 
      <telerik:RadSplitContainer> 
       <telerik:RadPaneGroup x:Name="radPaneGroup"> 
        <telerik:RadPane TitleTemplate="{StaticResource ContextMenuTemplate}" Title="Pane 1"> 
         <TextBlock Text="Some simple text here"/> 
        </telerik:RadPane> 
       </telerik:RadPaneGroup> 
      </telerik:RadSplitContainer> 
     </telerik:RadDocking.DocumentHost> 
    </telerik:RadDocking> 

</Grid> 

</Window> 

這裏是我的C#:

using System.Windows; 

namespace RadDockCloseButton1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     public static class RadDockingCommands 
     { 
      private static RoutedUICommand closeAllPanesButThisCommand; 

      public static RoutedUICommand CloseAllPanesButThisCommand 
      { 
       get 
       { 
        if (closeAllPanesButThisCommand == null) 
        { 
         closeAllPanesButThisCommand = new RoutedUICommand("Close all panes but this", "CloseAllPanesButThisCommand", typeof(RadDockingCommands)); 
        } 

        return closeAllPanesButThisCommand; 
       } 
      } 

      public static void OnCloseAllPanesButThis(object sender, ExecutedRoutedEventArgs e) 
      { 
       var pane = e.Parameter as RadPane; 
       if (pane != null) 
       { 
        var paneGroup = pane.PaneGroup; 
        if (paneGroup != null) 
        { 
         var panesToClose = paneGroup.EnumeratePanes().Where(x => !x.IsHidden && x.IsPinned); 
         foreach (var paneToClose in panesToClose) 
         { 
          if (paneToClose != pane) 
          { 
           paneToClose.IsHidden = true; 
          } 
         } 
        } 
       } 
      } 

      public static void OnCloseAllPanesButThisCanExecute(object sender, CanExecuteRoutedEventArgs e) 
      { 
       e.CanExecute = false; 
       var paneGroup = sender as RadPaneGroup; 
       if (paneGroup != null) 
       { 
        int childrenCount = paneGroup.EnumeratePanes().Count(x => !x.IsHidden && x.IsPinned); 

        if (childrenCount > 1) 
        { 
         e.CanExecute = true; 
        } 
        else 
        { 
         e.CanExecute = false; 
        } 
       } 
      } 
     } 
    } 
} 
+0

這聽起來更像你是想上下文菜單添加到對接選項卡,再:這是記事本+ +是如何工作的。或者你想添加2個「關閉」按鈕? –

+0

是的,這聽起來是對的。我只是希望它是一個可點擊的工具提示,而不是右鍵單擊,然後選擇執行此操作的方法。你知道關於添加上下文菜單到radDock關閉按鈕的任何事嗎? – JLott

+0

您可以使用[Telerik如何禁用(隱藏)關閉按鈕](http://www.telerik.com/help/wpf/raddocking-how-to-disable-the-close-button.html)。然後使用[Telerik如何添加按鈕標題](http://www.telerik.com/help/wpf/raddocking-how-to-add-buttons-to-the-pane-headers.html)。或者使用[Telerik如何將菜單項添加到RadPane](http://www.telerik.com/help/wpf/raddocking-how-to-add-menu-items-to-the-radpanes-menu.html) 。我已經將它們用於類似的場景。如果你能更清楚地描述你想要達到的目標,我可能會有更具體的想法。 –

回答

1

我添加了一個完整的完整代碼示例的第二個答案。請注意,此示例的全部內容直接來自Telerik How to Customize or Remove the RadPane's Menu。我只是把各個片段放在一起。換句話說,這是Telerik教程中的一個OOB實現。

XAML

<Window x:Class="so.Tel.RadPaneCloseAll.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:so.Tel.RadPaneCloseAll" 
     xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
     Title="MainWindow" 
     Width="525" 
     Height="350" 
     WindowStartupLocation="CenterScreen"> 
    <Window.Resources> 
     <DataTemplate x:Key="ContextMenuTemplate"> 
      <telerik:RadContextMenu InheritDataContext="False"> 
       <telerik:RadMenuItem Command="telerik:RadDockingCommands.Floating" 
            CommandParameter="{Binding}" 
            CommandTarget="{Binding}" 
            Header="{Binding Command.Text, 
                 RelativeSource={RelativeSource Self}}" 
            IsChecked="{Binding IsFloatingOnly}" /> 

       <telerik:RadMenuItem Command="telerik:RadDockingCommands.Dockable" 
            CommandParameter="{Binding}" 
            CommandTarget="{Binding}" 
            Header="{Binding Command.Text, 
                 RelativeSource={RelativeSource Self}}" 
            IsChecked="{Binding IsDockableOptionChecked}" /> 

       <telerik:RadMenuItem Command="local:RadDockingCommands.CloseAllPanesButThisCommand" 
            CommandParameter="{Binding}" 
            CommandTarget="{Binding}" 
            Header="{Binding Command.Text, 
                 RelativeSource={RelativeSource Self}}" /> 
      </telerik:RadContextMenu> 
     </DataTemplate> 

     <Style TargetType="telerik:RadPane"> 
      <Setter Property="ContextMenuTemplate" Value="{StaticResource ContextMenuTemplate}" /> 
     </Style> 
    </Window.Resources> 
    <Grid> 

     <telerik:RadDocking> 
      <telerik:RadDocking.DocumentHost> 
       <telerik:RadSplitContainer> 
        <telerik:RadPaneGroup> 
         <telerik:RadPane Header="Pane 1" /> 
         <telerik:RadPane Header="Pane 2" /> 
         <telerik:RadPane Header="Pane 3" /> 
         <telerik:RadPane Header="Pane 4" /> 
         <telerik:RadPane Header="Pane 5" /> 
        </telerik:RadPaneGroup> 
       </telerik:RadSplitContainer> 
      </telerik:RadDocking.DocumentHost> 
     </telerik:RadDocking> 

    </Grid> 
</Window> 

代碼隱藏

using System.Linq; 
using System.Windows; 
using System.Windows.Input; 
using Telerik.Windows.Controls; 

namespace so.Tel.RadPaneCloseAll 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      CommandManager.RegisterClassCommandBinding(
       typeof(RadPaneGroup), 
       new CommandBinding(
         RadDockingCommands.CloseAllPanesButThisCommand, 
         RadDockingCommands.OnCloseAllPanesButThis, 
         RadDockingCommands.OnCloseAllPanesButThisCanExecute)); 
     } 
    } 

    public static class RadDockingCommands 
    { 
     private static RoutedUICommand closeAllPanesButThisCommand; 

     public static RoutedUICommand CloseAllPanesButThisCommand 
     { 
      get 
      { 
       if(closeAllPanesButThisCommand == null) 
       { 
        closeAllPanesButThisCommand = new RoutedUICommand("Close all panes but this", 
                     "CloseAllPanesButThisCommand", 
                     typeof(RadDockingCommands)); 
       } 
       return closeAllPanesButThisCommand; 
      } 
     } 

     public static void OnCloseAllPanesButThis(object sender, ExecutedRoutedEventArgs e) 
     { 
      var pane = e.Parameter as RadPane; 
      if(pane != null) 
      { 
       var paneGroup = pane.PaneGroup; 
       if(paneGroup != null) 
       { 
        var panesToClose = paneGroup.EnumeratePanes().Where(x => !x.IsHidden && x.IsPinned); 
        foreach(var paneToClose in panesToClose) 
        { 
         if(paneToClose != pane) 
         { 
          paneToClose.IsHidden = true; 
         } 
        } 
       } 
      } 
     } 

     public static void OnCloseAllPanesButThisCanExecute(object sender, CanExecuteRoutedEventArgs e) 
     { 
      e.CanExecute = false; 
      var paneGroup = sender as RadPaneGroup; 
      if(paneGroup != null) 
      { 
       int childrenCount = paneGroup.EnumeratePanes().Count(x => !x.IsHidden && x.IsPinned); 

       if(childrenCount > 1) 
       { 
        e.CanExecute = true; 
       } 
       else 
       { 
        e.CanExecute = false; 
       } 
      } 
     } 
    } 
} 
+0

終於!當我右鍵單擊[x]按鈕時,我確實想要彈出菜單,但這會幫助我達到這一點。再次感謝您的幫助。 – JLott

1

基於您的代碼,你幾乎沒有。

<Window x:Class="RadDockCloseButton1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
     xmlns:local="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking" 
     Title="MainWindow" Height="350" Width="525"> 

應爲(注意local命名空間):

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

local命名空間指的是哪一類具有自定義條命令。在這種情況下,您已將該類添加到您的RadDockCloseButton1名稱空間中。

在構造函數註冊的命令:

public MainWindow() 
{ 
    InitializeComponent(); 

    CommandManager.RegisterClassCommandBinding(
    typeof(RadPaneGroup), 
    new CommandBinding(
     RadDockingCommands.CloseAllPanesButThisCommand, 
     RadDockingCommands.OnCloseAllPanesButThis, 
     RadDockingCommands.OnCloseAllPanesButThisCanExecute)); 
} 

並移動public static class RadDockingCommands類,以便它不是嵌套的MainWindow內。即

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     CommandManager.RegisterClassCommandBinding(
       typeof(RadPaneGroup), 
       new CommandBinding(
         RadDockingCommands.CloseAllPanesButThisCommand, 
         RadDockingCommands.OnCloseAllPanesButThis, 
         RadDockingCommands.OnCloseAllPanesButThisCanExecute)); 
    } 
} 

public static class RadDockingCommands 
{ 
    private static RoutedUICommand closeAllPanesButThisCommand; 

    // etc... 
} 

最後,用幾個窗格進行測試。無論是添加額外的窗格來樣,或使用此:

<telerik:RadDocking> 
    <telerik:RadDocking.DocumentHost> 
     <telerik:RadSplitContainer> 
      <telerik:RadPaneGroup> 
       <telerik:RadPane Header="Pane 1" /> 
       <telerik:RadPane Header="Pane 2" /> 
       <telerik:RadPane Header="Pane 3" /> 
       <telerik:RadPane Header="Pane 4" /> 
       <telerik:RadPane Header="Pane 5" /> 
      </telerik:RadPaneGroup> 
     </telerik:RadSplitContainer> 
    </telerik:RadDocking.DocumentHost> 
</telerik:RadDocking> 

如果仍然不能爲你工作,我會後整個工作樣本。

+0

是的......它仍然像RoutedUICommand一樣行事......我無法對你的所有幫助表示感謝...... – JLott

+0

看到我的其他答案有整個代碼清單。 –