2014-09-23 34 views
1

我是新的xaml。我想用文本而不是默認複選標記來創建自定義複選框。種類wpf自定義複選框與圖標內的文字

[Yes] Accept Value (checked state) 
[No] Accept Value  (unchecked state) 

我不想用帶有「靜態」文本的圖像。你可以請我轉到一些樣本或文章,這將涵蓋以下範圍。

回答

1

爲此,您可能需要修改默認控件模板。這裏有一個如何做一個簡單的例子:

<!--x:Key="PhoneCheckBox"--> 
<Style TargetType="CheckBox" x:Key="YesNoCheckBox"> 
    <Setter Property="Content" Value="Accept Value" /> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="CheckBox"> 
     <Grid Background="Transparent"> 

      <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="CommonStates"> 
       <VisualState x:Name="Normal"/> 
       <VisualState x:Name="MouseOver"/> 
       <VisualState x:Name="Pressed" /> 
       <VisualState x:Name="Disabled" /> 
      </VisualStateGroup> 
      <VisualStateGroup x:Name="CheckStates"> 
       <VisualState x:Name="Checked"> 
       <Storyboard> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckText" Storyboard.TargetProperty="Text"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="[Yes]" /> 
        </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
       </VisualState> 
       <VisualState x:Name="Unchecked" /> 
       <VisualState x:Name="Indeterminate" /> 
      </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 

      <Grid Margin="{StaticResource PhoneTouchTargetLargeOverhang}"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="32"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Border x:Name="CheckBackground" IsHitTestVisible="False" VerticalAlignment="Center" HorizontalAlignment="Left" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{StaticResource PhoneBorderThickness}" Height="32" Width="32"/> 
      <TextBlock x:Name="CheckText" Text="[No]" /> 
      <ContentControl x:Name="ContentContainer" Grid.Column="1" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="12,0,0,0" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" Foreground="{TemplateBinding Foreground}" /> 
      </Grid> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

我剛剛更換校驗符號與TextBlock那間「[是]」和「[無]」(也去掉了「按下」切換, 「殘疾」和「不確定」的視覺狀態故事板,簡潔起見)。

You can find the default control templates in your local SDK install.

使用上述,只是參考的樣式鍵:

<CheckBox Style="{StaticResource YesNoCheckBox}" /> 
+0

好的解決方案。不幸的是,沒有選項通過一些參數來綁定這個值,比如 2014-09-25 17:49:26

+0

@MikhailDenisov也很容易解決 - 儘管 - 你只需要擴展「CheckBox」控件並添加「ImageText」或其他任何你需要的依賴屬性。 – McGarnagle 2014-09-25 18:44:45

+0

我試圖通過添加新的參數來實現新的Windows Phone用戶控件,比如「public partial class ValueCheckBox:CheckBox」,出現錯誤:「SampleProj.Controls.ValueCheckBox的部分聲明不能指定不同的基類」。也不知道xaml的網格內容,據我瞭解,我需要從默認複選框中複製它,我可以從SDK中的elements.xaml(copypasting - 將無法使用)? – 2014-09-25 19:44:37