【问题标题】:WPF replace checkmark from original checkboxWPF 从原始复选框替换复选标记
【发布时间】:2015-01-24 22:39:29
【问题描述】:

我正在尝试用我自己的复选标记(实际上是路径)替换标准 WPF 复选框的复选标记。复选框应该看起来像标准的。

在哪里可以找到复选框的 Microsoft xaml 模板,以便在 xaml 中对其进行修改?还是有更优雅的方式来做到这一点?

注意:我已经在 MSDN (http://msdn.microsoft.com/en-us/library/ms752319%28v=vs.110%29.aspx) 上找到了一个模板,但它看起来完全不同。

问候, 黑图阿雷格

【问题讨论】:

    标签: c# wpf checkbox checkmark


    【解决方案1】:

    第一步:请打开新项目并记下复选框标签。

    <Window x:Class="WpfApplication8.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    
    <CheckBox Height="30" Width="200" Content="CheckBoxContent"></CheckBox>
    
    </Window>
    

    设计器视图看起来


    第二步:在Designer视图中----右键CheckBox->Edit Template->Edit A Copy

    第 3 步: 为样式命名 -> 选择资源窗口/checkobx ->ok

    最后,您可以通过更改 x:Name="markGrid" 中的路径来更改/替换路径

       <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
                   ---------------------------                
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type CheckBox}">
                            <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Border x:Name="checkBoxBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
    
    
                                    <!-- Change Path here-->
                                    <Grid x:Name="markGrid">
                                        <Path x:Name="optionMark" Data="F1M9.97498,1.22334L4.6983,9.09834 4.52164,9.09834 0,5.19331 1.27664,3.52165 4.255,6.08833 8.33331,1.52588E-05 9.97498,1.22334z" Fill="#FF212121" Margin="1" Opacity="0" Stretch="None"/>
                                        <Rectangle x:Name="indeterminateMark" Fill="#FF212121" Margin="2" Opacity="0"/>
                                    </Grid>
    
    
                                </Border>
                                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                            <ControlTemplate.Triggers>                                                             
                                <Trigger Property="IsChecked" Value="True">
    
                                  -----------------------------------------
    
                                </Trigger>
                                <Trigger Property="IsChecked" Value="{x:Null}">
    
                                 ---------------------------------------
    
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    更新:

    <Window.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="FocusVisualStyle">
                <Setter.Value>
                    <Style>
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="#FF707070"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border x:Name="checkBoxBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                <Grid x:Name="markGrid">
                                    <Path x:Name="optionMark" Data="F1M9.97498,1.22334L4.6983,9.09834 4.52164,9.09834 0,5.19331 1.27664,3.52165 4.255,6.08833 8.33331,1.52588E-05 9.97498,1.22334z" Fill="#FF212121" Margin="1" Opacity="0" Stretch="None"/>
                                    <Rectangle x:Name="indeterminateMark" Fill="#FF212121" Margin="2" Opacity="0"/>
                                </Grid>
                            </Border>
                            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasContent" Value="True">
                                <Setter Property="FocusVisualStyle">
                                    <Setter.Value>
                                        <Style>
                                            <Setter Property="Control.Template">
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Padding" Value="4,-1,0,0"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="checkBoxBorder" Value="#FFF3F9FF"/>
                                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="#FF5593FF"/>
                                <Setter Property="Fill" TargetName="optionMark" Value="#FF212121"/>
                                <Setter Property="Fill" TargetName="indeterminateMark" Value="#FF212121"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" TargetName="checkBoxBorder" Value="#FFE6E6E6"/>
                                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="#FFBCBCBC"/>
                                <Setter Property="Fill" TargetName="optionMark" Value="#FF707070"/>
                                <Setter Property="Fill" TargetName="indeterminateMark" Value="#FF707070"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" TargetName="checkBoxBorder" Value="#FFD9ECFF"/>
                                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="#FF3C77DD"/>
                                <Setter Property="Fill" TargetName="optionMark" Value="#FF212121"/>
                                <Setter Property="Fill" TargetName="indeterminateMark" Value="#FF212121"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Opacity" TargetName="optionMark" Value="1"/>
                                <Setter Property="Opacity" TargetName="indeterminateMark" Value="0"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="{x:Null}">
                                <Setter Property="Opacity" TargetName="optionMark" Value="0"/>
                                <Setter Property="Opacity" TargetName="indeterminateMark" Value="1"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    
    <Grid>
        <CheckBox Height="40" Width="200" Content="ok"></CheckBox>
    </Grid>
    

    【讨论】:

    • 您使用哪个 VisualStudio 版本?我在VS2010中找不到。
    • VS2013u4 express中没有这个选项。
    • @BlackTuareg 我正在使用 vs 2012,此选项在 vs2012 和 vs2013 专业版中可用..您可以使用表达式混合和 stylesnooper blog.tomaskafka.com/book/export/html/112 来提取样式。这可能有助于stackoverflow.com/questions/8825030/…跨度>
    • 我只得到一个使用 BulletDecorator 的模板。这不完全是我需要的,因为我不能只修改复选标记。但这可能是我能得到的最好的了。
    • 谢谢伙计,不知道有上下文菜单可以查看/编辑模板。我在网上查找模板,也必须找到正确的版本。
    【解决方案2】:

    链接的模板只是一些随机模板。这不是默认的CheckBox 模板。

    您可以自己轻松提取模板(完整来源请参见here,页面为俄语,但代码为英文 cmets):

    // get all control types
    Type typeControl = typeof(Control);
    List<Type> myTypes = new List<Type>();
    Assembly assembly = Assembly.GetAssembly(typeof(Control));
    foreach (Type type in assembly.GetTypes())
    {
        if (type.IsSubclassOf(typeControl) && !type.IsAbstract && type.IsPublic)
            myTypes.Add(type);
    }
    

    那么对于任何特定类型

    // Instantiate the type.
    ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
    Control control = (Control)info.Invoke(null);
    
    // Get the template.
    ControlTemplate template = control.Template;
    
    // Get the XAML for the template.
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    StringBuilder sb = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(sb, settings);
    XamlWriter.Save(template, writer);
    
    // Display the template.
    someControl.Text = sb.ToString();
    

    这是我的 CheckBox 模板:

    <?xml version="1.0" encoding="utf-16"?>
    <ControlTemplate TargetType="CheckBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
        <BulletDecorator Background="#00FFFFFF" SnapsToDevicePixels="True">
            <BulletDecorator.Bullet>
                <mwt:BulletChrome Background="{TemplateBinding Panel.Background}" BorderBrush="{TemplateBinding Border.BorderBrush}" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderPressed="{TemplateBinding ButtonBase.IsPressed}" IsChecked="{TemplateBinding ToggleButton.IsChecked}" />
            </BulletDecorator.Bullet>
            <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </BulletDecorator>
        <ControlTemplate.Triggers>
            <Trigger Property="ContentControl.HasContent">
                <Setter Property="FrameworkElement.FocusVisualStyle">
                    <Setter.Value>
                        <Style TargetType="IFrameworkInputElement">
                            <Style.Resources>
                                <ResourceDictionary />
                            </Style.Resources>
                            <Setter Property="Control.Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Rectangle Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" Margin="14,0,0,0" SnapsToDevicePixels="True" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
                <Setter Property="Control.Padding">
                    <Setter.Value>
                        <Thickness>4,0,0,0</Thickness>
                    </Setter.Value>
                </Setter>
                <Trigger.Value>
                    <s:Boolean>True</s:Boolean>
                </Trigger.Value>
            </Trigger>
            <Trigger Property="UIElement.IsEnabled">
                <Setter Property="TextElement.Foreground">
                    <Setter.Value>
                        <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                    </Setter.Value>
                </Setter>
                <Trigger.Value>
                    <s:Boolean>False</s:Boolean>
                </Trigger.Value>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    【讨论】:

    • 你拯救了我的一周!
    【解决方案3】:

    有一些工具可以从 System.Windows.Controls 中提取原始 XAML 模板,但最终取决于您,因为据我所知,Microsoft 可以在任何地方提供这些模板。您将需要重新定义整个 CheckBox 模板以实现您想要的。

    有一些网站列出了 .NET Framework 程序集的反射源代码,您可以尝试使用原始“generic.xaml”文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 2023-03-08
      相关资源
      最近更新 更多