WPF ToggleButton 和 DelegateCommand

标签 wpf togglebutton commandbinding delegatecommand

有没有办法确定 ToggleButton通过 DelegateCommand 选中/取消选中年代?

TIA,
麦克风

XAML 代码如下。我正在使用 ItemsControl并绑定(bind)到一个集合。我基本上想要一种在单击每个按钮时获取其切换状态的方法。

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <ItemsControl ItemsSource="{Binding Modifiers, Mode=TwoWay}">
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
                    <WrapPanel Margin="10" Width="{TemplateBinding Width}"
                               Height="{TemplateBinding Height}" 
                               FlowDirection="LeftToRight" IsItemsHost="true">
                    </WrapPanel>
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ToggleButton FontSize="18" Opacity="0.8"
                              Command="{Binding DataContext.ModifierToggleCommand, 
                                        RelativeSource={RelativeSource FindAncestor,
                                        AncestorType={x:Type Views:ModifiersView}}}" 
                              CommandParameter="{Binding}" Height="80" Width="200" Margin="5"
                              Content="{Binding Path=ModifierName}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

最佳答案

一个更简单的解决方案是绑定(bind) IsChecked属性到您的 ViewModel 的属性。这样你只需要检查属性(property)值(value)......

关于WPF ToggleButton 和 DelegateCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1777632/

相关文章:

wpf - TextBox.IsEnabled 绑定(bind)仅有效一次。为什么?

c# - WPF catch 单击网格及其所有子标签、文本框等。

c# - 使用父级的 DataContext(WPF - 动态菜单命令绑定(bind))

WPF 滚动查看器 : Horizontal scrolling doesn't work

c# - 用户控件的图片/图像

android - 共享首选项不读取按钮状态

javascript - 未选中时无法显示 div

ios - 如何创建三态开关按钮?

wpf - 上下文菜单中子菜单项的命令绑定(bind)

.net - WPF - 如何强制命令通过其 CommandBindings 重新评估 'CanExecute'