这是我实现这种功能的方式,但是不管它好不好 - 这是品味的问题。
主要XAML看起来如此:
<TabControl ItemsSource="{Binding TabItems}">
<TabControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Title}"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<local:ExecuteCommandAction Command="{Binding HeaderClickCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
没有ControlTemplate
,只是DataTemplate
与附加属性Interaction.Triggers
,其中前缀i
在这个字符串的定义:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
这个库可以在MS Blend SDK或库mvvm灯(在codeplex上)中找到。
此外,不要混淆EventTriggers
具有前缀i
和通用EventTriggers,他们在某一点不同,但我不知道两者的区别是什么,除了从自定义库工作EventTrigger
类Silverlight
。
在我的示例中,触发器订阅了事件MouseLeftButtonDown
,并在每次引发事件时调用特殊操作类。 这个动作类是一个自定义类,它是在代码中定义:
/// <summary>
/// Behaviour helps to bind any RoutedEvent of UIElement to Command.
/// </summary>
[DefaultTrigger(typeof(UIElement), typeof(System.Windows.Interactivity.EventTrigger), "MouseLeftButtonDown")]
public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
{
/// <summary>
/// Dependency property represents the Command of the behaviour.
/// </summary>
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter",
typeof(object), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
/// <summary>
/// Dependency property represents the Command parameter of the behaviour.
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command",
typeof(ICommand), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
/// <summary>
/// Gets or sets the Commmand.
/// </summary>
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
/// <summary>
/// Gets or sets the CommandParameter.
/// </summary>
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
/// <summary>
/// Invoke method is called when the given routed event is fired.
/// </summary>
/// <param name="parameter">
/// Parameter is the sender of the event.
/// </param>
protected override void Invoke(object parameter)
{
if (this.Command != null)
{
if (this.Command.CanExecute(this.CommandParameter))
{
this.Command.Execute(this.CommandParameter);
}
}
}
}
这是所有。现在该命令不会阻止tabcontrol选择项目。 代码隐藏来测试此XAML:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var items = new ObservableCollection<TabItemViewModel>
{
new TabItemViewModel("Item 1"), new TabItemViewModel("Item 2"), new TabItemViewModel("Item 3")
};
this.DataContext = new MainViewModel(){TabItems = items};
}
}
public class MainViewModel
{
public ObservableCollection<TabItemViewModel> TabItems { get; set; }
}
public class TabItemViewModel
{
public TabItemViewModel(string title)
{
this.Title = title;
this.HeaderClickCommand = new RelayCommand(() => MessageBox.Show("Clicked "+this.Title));
}
public string Title { get; set; }
public RelayCommand HeaderClickCommand { get; set; }
}
要呼叫选择了一个项目,只有当该命令,改变此代码:
<local:ExecuteCommandAction Command="{Binding HeaderClickCommand}"
CommandParameter="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem}}"/>
而这个(第二个参数是CanExecute
代表,它支票IsSelected == true
):
this.HeaderClickCommand = new RelayCommand<bool>(b => {/*???*/}, b => b == true);
你想要一个实际的按钮作为tabheader,或者你只是想执行一个命令tabchange吗? – 2011-03-21 18:32:56
嗨,我打算在标题中放一个按钮。这个想法是,如果用户单击标题,它会通过虚拟机中的更改来更新标签内容。谢谢 – Sidebp 2011-03-21 19:58:44
该按钮对此功能不太好,最好使用MouseLeftButtonDown事件。我的答案必须帮助你理解我的意思。 – vorrtex 2011-03-21 20:50:28