2009-10-29 70 views
2

我正在使用複選框替換單選按鈕的樣式。我知道你會說這是一個壞主意,但它確實需要這樣工作。無論如何,使用Expression Blend我能夠提取CheckBox的樣式並將其應用於RadioButton。我現在唯一的問題是,現在當它繪製時沒有邊界。誰能告訴我爲什麼?下面是代碼(這已被更新,因爲原始POST):爲什麼我的自定義WPF RadioButton看起來不正確?

<Window x:Class="WpfApplication1.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Royale" 
Title="Customized RadioButton" Width="496" ShowInTaskbar="True" ResizeMode="NoResize" Height="105"> 
<Window.Resources> 
    <SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F"/> 

    <Style x:Key="RadioButtonCheckBoxStyle" TargetType="{x:Type RadioButton}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="Background" Value="#F4F4F4"/> 
     <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type RadioButton}"> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
         <BulletDecorator Background="Transparent"> 
          <BulletDecorator.Bullet> 
           <Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/> 
          </BulletDecorator.Bullet> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/> 
         </BulletDecorator> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="HasContent" Value="true"> 
          <Setter Property="Padding" Value="4,0,0,0"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <CheckBox Height="16" Name="checkBox1" Width="120">CheckBox</CheckBox> 
    <RadioButton Height="16" Name="radioButton1" Width="120">RadioButton</RadioButton> 
    <RadioButton Content="RadioButton with CheckBox Style" Margin="4" Style="{DynamicResource RadioButtonCheckBoxStyle}" FocusVisualStyle="{x:Null}" GroupName="Header" IsChecked="False" IsEnabled="True" HorizontalAlignment="Center" /> 
</StackPanel> 

alt text http://img260.imageshack.us/img260/65/33733770.jpg

要了解此顯示,請粘貼到Visual Studio中。您會看到Customized RadioButton看起來不正確。它缺少CheckBox通常具有的常規3D效果。我不關心任何內容(我不會有任何內容),我只是希望它看起來像一個普通的CheckBox。

+0

如果您想要複選框的完整外觀,只需使用複選框可能更容易,並且在選擇其他複選框時手動取消選中該組中的其他框。雖然下面的答案可能會讓你的邊框回來,但它不一定會產生完整的3D效果。 – 2009-10-29 20:31:55

+0

看看:http://stackoverflow.com/questions/3975333/how-do-i-make-a-checkbox-look-like-a-radiobutton – 2012-05-04 21:56:44

回答

2

由於您重寫了模板,因此您需要自己附加到該類的屬性。

聲明在以下的時尚模板邊框:

<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/> 

此外,您已經有了一個小bug。你已經用你的風格多次定義了「BorderBrush」的setter。

編輯:看到你的形象後,這裏是你真正的問題:

<Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/> 

你失蹤在你的子彈了borderThickness。它有效地將你的厚度設置爲0,這就是爲什麼你沒有看到任何東西。

+0

我在哪裏添加代碼?我試過放入ControlTemplate,但它不能編譯。謝謝你的幫助,威爾。 – skb 2009-10-29 16:58:52

+0

這取決於你想要的邊框包圍。我假設在你的'RadioButtonCheckBoxStyle'中聲明的ControlTemplate內,正如Drew Marsh的回答所指出的,但你可以在你的''中嘗試它。 – 2009-10-29 17:10:34

+1

看到我的編輯,你在子彈聲明中缺少'BorderThickness'。 – 2009-10-29 17:36:37

0

圖片可能對您有所幫助,但您認爲邊界是在哪裏?你是否期待整個內容的邊界?根據您使用的模板,應該有唯一的邊框在檢查標記的位置。如果你想周圍的所有內容的邊界,那麼你需要一個Border添加到ControlTemplate像這樣:

  <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type RadioButton}"> 
         <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
          <BulletDecorator Background="Transparent"> 
           <BulletDecorator.Bullet> 
            <Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/> 
           </BulletDecorator.Bullet> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignme nt}" RecognizesAccessKey="True"/> 
         </BulletDecorator> 
          <ControlTemplate.Triggers> 
            <Trigger Property="HasContent" Value="true"> 
            <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/> 
            <Setter Property="Padding" Value="4,0,0,0"/> 
           </Trigger> 
           <Trigger Property="IsEnabled" Value="false"> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 

旁註:你對複選框本身設置FocusVisualStyle="{x:Null}",但你的模板改變這個屬性其HasContent屬性觸發器。如果你真的不想要一個FocusVisualStyle,你應該刪除那個setter。

+0

Will Eddins的修訂答案是否正確,您在BulletChrome上缺少BorderThickness。 – 2009-10-29 18:22:08

相關問題