wpf - 无法命令将切换按钮绑定(bind)为控件模板

标签 wpf templates xaml mvvm

这是代码:

   <Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>

<Window.Resources>

    <Style x:Key="RadioToggleButtonStyle" TargetType="RadioButton">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <ToggleButton Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="25" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

</Window.Resources>

<Grid>
    <RadioButton Name="radioButton1"
                 Height="29"
                 Margin="193,195,0,0"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 **Command="{Binding Path=RadioClickCommand}"**
                 Content="RadioButton"
                 Style="{StaticResource RadioToggleButtonStyle}" />

</Grid>

在 MainWindowViewModel :我有以下命令注册
 public ICommand RadioClickCommand
    {
        get { return new RelayCommand(RadioClickExecute); }
    }
 private void RadioClickExecute()
    {

    }

按钮单击永远不会触发命令绑定(bind)。
任何帮助,将不胜感激。

最佳答案

你忘记了你必须定义一个 CommandToggleButton

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type RadioButton}">
            <ToggleButton Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                          Command="{Binding Command, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/>
        </ControlTemplate>
    </Setter.Value>
</Setter>

关于wpf - 无法命令将切换按钮绑定(bind)为控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293737/

相关文章:

c++ - 尝试从元组中删除最后一个类型失败

c# - WPF ComboBox 选择导致第二个 ComboBox 中的选择

wpf - 如何强制图像在 WPF 中保持纵横比?

wpf - 如何使用 WPF 实现无边框窗口?

javascript - Angular 1 将数据绑定(bind)到脚本模板

wpf - 如何在WPF选项卡控件中创建梯形选项卡

参数替换的 C++ 规则

c# - 使用 mvvm 在 wpf 数据网格中绑定(bind)级联组合框项目源

c# - 在EntityContainer中未定义EntitySet。 EF WPF MVVM

wpf - {Binding PropertyName} 和 {Binding Path=PropertyName} 之间的区别