2012-04-24 99 views
0

下面是代碼:無法命令綁定切換按鈕控件模板

<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() 
    { 

    } 

按鈕點擊從未觸發命令綁定。 任何幫助,將不勝感激。

回答

5

你忘了你的風格,你必須定義的ToggleButton

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

哎呀一個Command! ..感謝一百萬,真的很感謝你的及時迴應 – Anees 2012-04-24 08:18:56