c# - 覆盖 Telerik 关闭按钮

标签 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;
                    }
                }
            }
        }
    }
}

最佳答案

我正在添加带有完整代码示例的第二个答案。请注意,此示例的全部内容直接取自 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;
                }
            }
        }
    }
}

关于c# - 覆盖 Telerik 关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130019/

相关文章:

c# - 使用C#从字符串中提取多个值

c# - 在我的 WPF 应用程序中捕获没有焦点的按键事件

c# - 不理解 wpf 更新动态创建的图像控件可见性每秒变化的行为

wpf - 与 WPF 中的数学运算绑定(bind)

.net - 如何在 xaml 中绑定(bind)到动态 xpath?

c# - 微软客户关系管理 : Create Address Record Throws Incorrect Attribute Value Type Error

c# - 拆分逗号分隔的字符串并比较列表中的每个值

c# - ASP.NET Core依赖注入(inject): The Difference Between Factory and Instance?

c# - 我可以从具有多个轴的 oxyplot 图表中删除裁剪吗?

wpf - 如何防止根据条件选择 TreeViewItem