覆盖Telerik关闭按钮

时间:2013-01-02 22:10:24

标签: c# wpf xaml telerik styles

我在底座上有一个telerik关闭按钮,我希望覆盖它的样式,以允许用户选择他们想要关闭的内容。

以Notepad ++为例..有一个“关闭”和一个“关闭所有这个”选项。

这正是我想用这个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;
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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>

如果这仍然不适合您,我会发布整个工作样本。

答案 1 :(得分: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;
                }
            }
        }
    }
}
相关问题