WPF ToggleButton 和 DelegateCommand
有没有办法通过 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 DelegateCommand
s?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个更简单的解决方案是将 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...您能否在 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?