gpt4 book ai didi

c# - 在 DataTemplate 中的按钮上设置命令/操作

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:20 27 4
gpt4 key购买 nike

我为 TabControl 创建了一个新样式。在 TabItem 上我有一个关闭按钮。现在,我希望用户能够单击该关闭按钮以关闭事件选项卡。

<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>

在模板中通过适当的代码很好地设置了关闭 TabItem 操作的位置。但是,如果我想在选项卡上包含第二个按钮,该按钮可以保存远离模板的自定义图像集,该怎么办。 我如何做到这一点以及如何处理在模板外单击此按钮?可以执行此操作的 TabItem 示例是 VS2012... p>

TabItemVS2012

感谢您的宝贵时间。

最佳答案

killer 摄像头!

我对 DataTemplate 上的 MenuItem 也经历过同样的事情。

我所做的是在我的 Style 中为类型 Button 添加一个 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>

记住 {Binding Comando} 是我的 ViewModel 的 ICommnd 属性,如下所示:

private ICommand _Comando;
public ICommand Comando
{
get
{
return _Comando;
}
set
{
_Comando = value;
OnPropertyChanged("Comando");
}
}

在我看来,我可以像这样设置任何我想要的 Comando (ICommand):

ViewModel.MenuSubItem.Add(new MenuViewModel { Texto = "xxxxx", Comando = WhateverCommand });

希望对您有所帮助!

关于c# - 在 DataTemplate 中的按钮上设置命令/操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com