无法命令绑定切换按钮作为控件模板

时间:2012-04-24 07:41:44

标签: 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()
    {

    }

按钮单击从不触发命令绑定。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:5)

你忘记了自己的风格,你必须定义一个Command的{​​{1}}

ToggleButton