【问题标题】:How to customize the style of CheckBox in WPFWPF中如何自定义CheckBox的样式
【发布时间】:2013-04-25 03:29:03
【问题描述】:

我刚开始学习WPF,所以我对样式和模板不熟悉。 我想自定义一个CheckBox 和一个Image 和两个Labels,如下所示:

我该怎么办?

.xaml

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel>
        <CheckBox Width="150" 
                  Height="40" 
                  Margin="4" 
                  Padding="4,0,0,0">
            <Grid Background="#FFEEEEEE" 
                  Width="130"
                  MaxWidth="Infinity">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image Grid.Row="0"
                       Grid.RowSpan="2"
                       Grid.Column="0"
                       Margin="5" 
                       Source="/WpfApplication4;component/Images/LargeIcon.png" />
                <Label Grid.Row="0"
                       Grid.Column="1" 
                       Padding="0">
                    Label1
                </Label>
                <Label Grid.Row="1"
                       Grid.Column="1" 
                       Padding="0">
                    Label2
                </Label>
            </Grid>
        </CheckBox>
    </StackPanel>
</Window>

编辑:

.xaml

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2006" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <Style x:Key="MyCheckBox" 
               TargetType="{x:Type CheckBox}">
            <Setter Property="Width" Value="150"/>
            <Setter Property="Height" Value="40"/>
            <Setter Property="Margin" Value="4"/>
            <Setter Property="Padding" Value="4,0,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <DockPanel Background="#FFEEEEEE" 
                                   Height="34"
                                   Width="130">
                            <Image DockPanel.Dock="Left"
                                   Source="/WpfApplication4;component/Images/LargeIcon.png"
                                   Margin="5" />
                            <TextBlock DockPanel.Dock="Top" Text="Label1" />
                            <TextBlock DockPanel.Dock="Top" Text="Label2" />
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

在.xaml中使用

<CheckBox Style="{StaticResource ResourceKey=MyCheckBox}" />  

有些东西出现了,但小网格消失了,像这样:

我该怎么办?

【问题讨论】:

  • 我找不到别人的答案。怎么了?会不会不小心删了?
  • 对不起,我正在编辑我的答案:)

标签: wpf templates checkbox styles


【解决方案1】:

DockPanel 可能是此布局的最佳选择

例子:

<CheckBox>
   <DockPanel Height="34">
      <Image DockPanel.Dock="Left" Source="/WpfApplication4;component/Images/LargeIcon.png" Margin="2" />
      <TextBlock DockPanel.Dock="Top" Text="Label1" />
      <TextBlock DockPanel.Dock="Top" Text="Label2" />
   </DockPanel>
</CheckBox>

编辑:

您似乎仍想使用默认的Checkbox Template,但只需覆盖Style 中的Content

例子:

<Style x:Key="MyCheckBox" TargetType="{x:Type CheckBox}">
    <Setter Property="Width" Value="150"/>
    <Setter Property="Height" Value="40"/>
    <Setter Property="Margin" Value="4"/>
    <Setter Property="Padding" Value="4,0,0,0"/>
    <Setter Property="Content">
        <Setter.Value>
            <DockPanel Background="#FFEEEEEE" Width="130"  MaxWidth="Infinity">
                <Image DockPanel.Dock="Left" Source="Capture.png" Margin="5" />
                <TextBlock DockPanel.Dock="Top" Text="Label1" />
                <TextBlock DockPanel.Dock="Top" Text="Label2" />
            </DockPanel>
        </Setter.Value>
    </Setter>
</Style>

结果:

【讨论】:

  • 非常感谢。我可以将小网格移动到面板中的其他位置吗?
  • 以及如何动态修改CheckBox中TextBox的文本?
  • 我试图做类似的事情,但是我的 DataGrid 是动态生成的,并且复选框是其中的第一列。所以我试图设置这样的风格。 &lt;UserControl.Resources&gt; &lt;Style TargetType="{x:Type CheckBox}"&gt;&lt;Setter Property="VerticalAlignment" Value="Center" /&gt; &lt;/Style&gt; &lt;/Usercontrol.Resoruces&gt; 不使用键,因为我可以让所有复选框垂直居中。但这不起作用。有什么提示吗?
猜你喜欢
  • 2022-01-26
  • 2012-05-06
  • 2010-11-23
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 2011-02-07
相关资源
最近更新 更多