2017-09-04 27 views
0

我有2個按鈕應該充當開關,所以我使用單選按鈕。但單選按鈕的樣式應該看起來像切換按鈕,這意味着它不應該有一個圈子來檢查。Radibutton風格作爲切換按鈕在xaml

我搜索了很長時間沒有有用的資源。

<Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type ToggleButton}}"> 
      <Setter Property="HorizontalAlignment" Value="Center"/> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
     </Style> 


    <Style TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Background" Value="{StaticResource BgColor}" /> 
     <Setter Property="Foreground" Value="{StaticResource FgColor}" /> 
     <Setter Property="BorderBrush" Value="Transparent" /> 
     <Setter Property="Focusable" Value="False" /> 
     <Setter Property="Margin" Value="0" /> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Foreground" Value="{StaticResource HoverColor}" /> 
      </Trigger> 
      <Trigger Property="IsPressed" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource TextBlack}" /> 
      </Trigger> 
      <Trigger Property="IsChecked" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource TextBlack}" /> 
       <Setter Property="Foreground" Value="{StaticResource HoverColor}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

但它留下了空間,黑色圓(無線電屬性)和鼠標懸停時,它顯示了一圈,檢查..

誰能知道要實現它的最簡單的方法..

回答

0

你真正需要做的是確定造型爲你的單選按鈕,看起來像這樣:

<Style TargetType="{x:Type RadioButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <ToggleButton Content="{Binding Path=(Content), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}" 
           IsChecked="{Binding Path=(IsChecked), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

這就是你的核心。您可以添加您需要添加的任何其他Setters,或ControlTemplate中的樣式ToggleButton,使其看起來像您想要的樣子。

+0

同樣出現在App.Xaml中,我提到..但我面臨的問題是:1)默認模式:它不顯示任何預期的圓形,但留下的空間2)在鼠標懸停:圓形再回到 – srinivas

+0

好吧,我的解決方案出於某種原因只適用於沒有明確的樣式應用於此控件...讓我編輯它來解決您的問題。 –