WPF ToggleButton 和 DelegateCommand

发布于 2024-08-12 05:37:57 字数 1567 浏览 3 评论 0原文

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

TIA, Mike

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

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

Is there a way to determine if a ToggleButton is Checked/Unchecked via DelegateCommands?

TIA,
mike

XAML code below. I'm using ItemsControl and binding to a collection. I'm basically wanting a way to get the toggle status of each button when it's clicked.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-08-19 05:37:57

一个更简单的解决方案是将 IsChecked 属性绑定到 ViewModel 的属性。这样你只需要检查财产价值......

A simpler solution would be to bind the IsChecked property to a property of your ViewModel. That way you just have to check the property value...

樱&纷飞 2024-08-19 05:37:57

您能否在 XAML 中以声明方式指定 CommandParameter 并使用元素绑定来使用切换的当前值填充该值?

Could you specify the CommandParameter declaratively in the XAML and use an element binding to populate the value with the current value of the toggle?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文