2011-05-31 73 views
1

我有幾個按鈕作爲我的WPF應用程序中的菜單。WPF MVVM中Button.IsSelected類型的屬性?

這種情況就像網站中的菜單。

當我單擊其中一個按鈕時,我希望該按鈕樣式與其他人不同,當我選擇另一個時,上一個按鈕應該是正常的,並且選定的樣式應該應用於此選定按鈕。

你能告訴我如何通過ControlTemplate實現這一點,還是必須維護一個IsSelected屬性,讓我們知道哪個按鈕被選中?

感謝,

VJ

回答

6

你可以用RadionButton嘗試。下面的示例將爲RadioButton創建一個Flat按鈕外觀。

<Window x:Class="WpfApplication8.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="472"> 
<Window.Resources> 
    <Style TargetType="{x:Type RadioButton}"> 
     <Setter Property="Focusable" 
       Value="False" /> 
     <Setter Property="GroupName" 
       Value="filter" /> 
     <Setter Property="IsTabStop" 
       Value="False" /> 
     <Setter Property="VerticalAlignment" 
       Value="Center" /> 
     <Setter Property="HorizontalAlignment" 
       Value="Center" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type RadioButton}"> 
        <ControlTemplate.Resources> 

         <Style TargetType="{x:Type TextBlock}"> 
          <Setter Property="VerticalAlignment" 
            Value="Center" /> 
          <Setter Property="HorizontalAlignment" 
            Value="Center" /> 
         </Style> 

        </ControlTemplate.Resources> 
        <Border x:Name="PART_border" 
          CornerRadius="2" 
          Margin="2" 
          Background="Transparent" 
          BorderThickness="1" 
          BorderBrush="{x:Static SystemColors.ControlDarkBrush}" 
          SnapsToDevicePixels="True"> 
         <StackPanel Orientation="Horizontal" 
            HorizontalAlignment="Center" VerticalAlignment="Center"> 
          <ContentPresenter x:Name="PART_content" /> 
         </StackPanel> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsChecked" 
           Value="True"> 
          <Setter TargetName="PART_content" 
            Property="TextBlock.FontWeight" 
            Value="Bold" /> 
          <Setter TargetName="PART_border" 
            Property="Background"> 
           <Setter.Value> 
            <LinearGradientBrush StartPoint="0,0" 
                 EndPoint="0,1"> 
             <GradientStop Color="Black" 
                 Offset="0" /> 
             <GradientStop Color="white" 
                 Offset="1" /> 
            </LinearGradientBrush> 
           </Setter.Value> 
          </Setter> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid> 
    <StackPanel Orientation="Horizontal" > 
    <RadioButton Height="30" Width="100" Content="First"></RadioButton> 
    <RadioButton Height="30" 
       Width="100" 
       Content="Second"></RadioButton> 
    <RadioButton Height="30" 
       Width="100" 
       Content="First"></RadioButton> 
     </StackPanel> 
</Grid> 

的單選按鈕與圖片只是看看Matt的博客

http://madprops.org/blog/wpf-killed-the-radiobutton-star/

1

您應使用內置的視覺狀態的處理和創建國家/樣式XAML。

在你的具體情況下,你看到的是一組RadioButton,所以你不必編寫任何代碼來處理狀態之間的切換。

+0

我們怎樣才能樣式單選按鈕進入按鈕? – 2011-05-31 11:12:45

+0

如何使用ToggleButton。我能從這做到這一點嗎? – 2011-05-31 11:14:10

+0

你有Expression Blend嗎?這是最容易做到這一點的Blend,在那裏你可以添加一個RadioButton>右鍵單擊>編輯模板>編輯一個副本...,這將生成默認的控制模板爲你準備好自定義看起來像一個按鈕(或甚至是一個ToggleButton) – dain 2011-05-31 11:16:10