我在码头上有一个 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;
}
}
}
}
}
}