C# 在DataTemplate中的按钮上设置命令/操作
我已经为C# 在DataTemplate中的按钮上设置命令/操作,c#,wpf,datatemplate,C#,Wpf,Datatemplate,我已经为选项卡控件创建了一种新样式。在选项卡item上,我有一个关闭按钮。现在,我希望用户能够单击关闭按钮关闭活动选项卡 <Style x:Key="StudioTabControl" TargetType="{x:Type TabControl}"> <Style.Resources> <Style TargetType="{x:Type TabItem}"> <Setter Property="Focu
选项卡控件
创建了一种新样式。在选项卡item
上,我有一个关闭按钮。现在,我希望用户能够单击关闭按钮关闭活动选项卡
<Style x:Key="StudioTabControl" TargetType="{x:Type TabControl}">
<Style.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Height="20"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="35"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"
Margin="10,0,10,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ContentSource="Header" />
<Button Grid.Column="1"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command= // CLOSE COMMAND OPERATION HERE.
DockPanel.Dock="Right">
...
...
我知道我可以做像这样的事情
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Actions:CloseTabItemAction TabItem="{Binding RelativeSource={RelativeSource AncestorType=TabItem}}"
TabControl="{Binding RelativeSource={RelativeSource AncestorType=TabControl}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
在模板中设置close TabItem操作时,请使用适当的代码。但是,如果我想在选项卡上包含第二个按钮,可以在远离模板的地方保存自定义图像集,该怎么办呢我如何才能做到这一点,以及如何处理在模板外单击此按钮?可以做到这一点的TabItem
示例是VS2012
谢谢您的时间。Killercam
我在DataTemplate上使用了MenuItem
我所做的是在我的Style
中为type按钮添加一个Setter Property=Command
。
这是我的密码:
<DataTemplate x:Key="Whatever">
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Comando}" />
<Setter Property="CommandParameter" Value="{Binding Texto}"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<StackPanel Orientation="Horizontal">
<!--<Image Source="{Binding Imagem}" />-->
<AccessText Text="{Binding Texto}" />
</StackPanel>
</DataTemplate>
在我看来,我可以像这样设置任何我想要的Comando(ICommand):
private ICommand _Comando;
public ICommand Comando
{
get
{
return _Comando;
}
set
{
_Comando = value;
OnPropertyChanged("Comando");
}
}
ViewModel.MenuSubItem.Add(new MenuViewModel { Texto = "xxxxx", Comando = WhateverCommand });
希望我能帮上忙