- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这感觉像是一个非常基本的问题,但我相信有更好的方法来做到这一点。我的 UI 中有一个 Button
,它选择一个特定的选项卡并从 ViewModel
Command
这是当前代码(可以正常工作):
XAML:
<Button Content="Button!" Click="OnButtonClick" Command="{Binding WhateverCommand}" />
代码隐藏:
private void OnButtonClick(object sender, RoutedEventArgs e)
{
theTab.IsSelected = true;
}
难道没有任何更简洁、仅使用 XAML 的方法来执行该 UI 操作吗?我在想类似的事情:
<Button Content="Button!" Click="OnButtonClick" Command="{Binding WhateverCommand}">
<Button.Triggers>
<EventTrigger RoutedEvent="Click">
<Setter TargetName="theTab" Property="IsSelected" Value="True" />
</EventTrigger>
</Button.Trigger>
</Button>
但不幸的是,EventTrigger
似乎不支持 Click
事件。为什么这样?在 WPF 中工作了几年后,我有时仍然对触发器感到困惑,这几乎可以概括它。尝试构建时,Setter
行出现错误:
A value of type 'Setter' cannot be added to a collection or dictionary of type 'TriggerActionCollection'.
谢谢!
编辑因为有人问我窗口的 XAML 结构,它看起来像这样:
<DockPanel LastChildFill="True">
<Ribbon DockPanel.Dock="Top">
<Button Content="Button!" Click="OnButtonClick" Command="{Binding WhateverCommand}" />
</Ribbon>
<TabControl>
<TabItem x:Name="theTab" />
</TabControl>
</DockPanel>
最佳答案
错误总结。您不能在 EventTrigger
中使用 Setter
。如果您想在 XAML 中执行此操作,您可以使用 Storyboard
,它会在按下按钮时选择给定的选项卡。像这样:
<Button Content="Click">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="theTab" Storyboard.TargetProperty="IsSelected">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
关于c# - 在 XAML 中处理 Button 的 Click 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011869/
我遇到过这个 html: 上面的html和这个有什么区别: 最佳答案 来自MDN page on the tag : 对于 type 的属性标签,可能的值是: 提交:按钮将表单数据提交给服务器
Button button= (Button) findViewbyID(R.id.button); 和 Button button = new Button(this); 有什么区别? 最佳答案 有
我是一名优秀的程序员,十分优秀!