我正在使用Microsofts RibbonControlLibrary作为带有几个RibbonCommands的Ribbonmenu . 我有一个TabControl应该包含每个按下的功能区按钮的选项卡 . 例如 . 我点击RibbonCommand 2,我想创建一个名为“Tab2”的标签,如果TabControl中还没有它,那么它应该是焦点 .
我已经实现了http://www.codeproject.com/KB/WPF/WpfTabCloseButton.aspx,非常好 .
我想我仍然没有得到WPF的事件机制 . 我的代码强烈地调整了Ribboncontrol的示例项目 .
这就是我实现命令的方式:
<Window.CommandBindings>
<CommandBinding Command="me:AppCommands.Protokoll" Executed="RibbonButton_Click" />
</Window.CommandBindings>
RibbonControl内的按钮如下所示 .
<r:RibbonButton Command="me:AppCommands.Protokoll" />
AppCommands.cs
public class AppCommands
{
public static RibbonCommand Protokoll
{
get { return (RibbonCommand)Application.Current.Resources["ProtokollCommand"]; }
}
}
ResourceDictionary中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary">
<r:RibbonCommand x:Key="ProtokollCommand"
LabelTitle="foo"
ToolTipTitle="foo"
ToolTipDescription="foo" />
</ResourceDictionary>
我现在想要写的是一个方法,为每个按钮单击创建一个Tab,其中创建的tabpage包含特定的usercontrols . 我不知道,如何最好地实现CommandButtons和Tabs之间的映射 . 我认为最好只使用一个click-eventhandler而不是每个按钮的eventhandler . Questions:
1) 如何找出引发事件的唯一按钮?我've only managed to get the information in the ResourceDictionary. Unfortunatly there is no such property as uniqueID or something like this. I'已完成此操作:
private void RibbonButton_Click(object sender, RoutedEventArgs e)
{
// ((Microsoft.Windows.Controls.Ribbon.RibbonCommand)(((System.Windows.Controls.Button)(e.OriginalSource)).Command))
}
我不想创建一个基于LabelTitle属性的映射,它应该用于显示 .
2) 我发现这个WPF/C#: How does one reference TabItems inside a TabControl?向我展示了如何查询TabControl的Items集合 . 这是一个很好的尝试还是有更好的尝试?
3)
我应该如何在RibbonCommand和应该显示的TabPage控件之间创建映射 . 当然我可以使用像这样的词典
Dictionary<String, fooContainer>
// ...
public class fooContainer()
{
public String TabHeaderString {get;set;}
// other properties
}
或者WPF对这些ResourceDictionaries提出了更好的方法,到目前为止我还没有理解 .
1 回答
由于您使用的是
Command
管理标签,我想你可以以一种有意义的方式设置的每个Button
的CommandParameter
属性,因此,这是可能的,你需要实现不同的操作之间进行区分(即它可能是一个Enum
,如果您已经知道需要公开的选项卡类型,或者更复杂的对象,以防这些选项卡可以动态创建) .顺便说一下,
RibbonButton_Click
方法的签名对我来说看起来不正确,因为传递给方法的参数应该是ExecutedRoutedEventHandler
类型 . 这样做将允许您通过args
变量的Parameter
属性访问Button
级别指定的CommandParameter
. 下面的代码修改:1. 为
RibbonButton
添加一个合适的CommandParameter
2. 将处理程序更改为
Execute
事件,以匹配正确的签名,并根据您的需要处理参数直接回答你的问题:
1. 你不应该需要知道哪个按钮的情况下提出的
Command
,正确的方法是使用一个CommandParameter
以确定要执行(该RibbonButton
是Command
的刚才主持人什么样的行动,不使感谢我将应用程序的逻辑设计到特定的UI实现中 .2. 如果你真的需要查询
TacControl
的TabItem
孩子,你提供的链接当然是要走的路 .3. 一旦你已经确定你需要执行的操作,你可以检索的实例
TabItem
你需要通过注册你以前创建的(即你可以使用到Enum
值与TabItem
实例关联适当Dictionary
) . 另一种可能的方式是使用在每个TabItem
的Tag
属性,将其设置为一个适当的值Enum
和使用下面的方法来检索适当TabItem
(注意kind
是在前面的段中定义的变量):