2

標準の WPF チェックボックスのチェックマークを自分のチェックマーク (実際にはパス) に置き換えようとしています。チェックボックスは標準のもののように見えるはずです。

チェックボックスの Microsoft xaml テンプレートはどこにありますか? xaml で変更できますか? または、これを行うためのよりエレガントな方法はありますか?

注: MSDN ( http://msdn.microsoft.com/en-us/library/ms752319%28v=vs.110%29.aspx ) で既にテンプレートを見つけましたが、見た目がまったく異なります。

よろしく、 ブラックトゥアレグ

4

3 に答える 3

3

リンクされたテンプレートは、単にランダムなテンプレートです。デフォルトのCheckBoxテンプレートではありません。

自分でテンプレートを簡単に抽出できます (完全なソースについては、こちらを参照してください。ページはロシア語ですが、コードには英語のコメントが付いています)。

// 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>
于 2014-11-26T15:36:43.560 に答える
-1

System.Windows.Controls から元の XAML テンプレートを抽出するツールがありますが、最終的には、Microsoft がどこでもそれを提供していることを知っている限り、それはあなた次第です。CheckBox目的を達成するには、テンプレート全体を再定義する必要があります。

元の "generic.xaml" ファイルで試すことができる .NET Framework アセンブリの反映されたソース コードをリストしている Web サイトがいくつかあります。

于 2014-11-26T15:13:06.050 に答える