English 中文(简体)
创建按钮模板
原标题:Creating a button Template

我试图为一个按钮创建一个模板, 我可以在窗体上一次又一次地使用这个按钮。

按钮 , 我要它包含一个 Grid , 底行内有两个行和自定义的文本块 。

这是我到现在为止得到的, 但我不认为这是对的, 因为我想从按钮元素中设置文本。

<ControlTemplate TargetType="Control">
    <Grid Width="444">
        <Grid.RowDefinitions>
            <RowDefinition Height="51" />
            <RowDefinition Height="36" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="#286c97"></Grid>
        <Grid Grid.Row="1" Background="#5898c0">
            <TextBlock Grid.Row="1" FontFamily="Segoe UI" FontSize="12" Text="{TemplateBinding Content}" />
        </Grid>
    </Grid>
</ControlTemplate>

然后打电话给模板 我希望我可以去:

<Button Content="This is the text" />

但不幸的是,这不起作用。我是否还要用其他模板来传递文本价值?

最佳回答

要让它生效,有一个叫做 ContentPresententer 的控制。 位于您想要的模板中。 但记住, 它可以是任何东西, 文本, 图像或其他一系列的控制, 您的 < code> 按钮 或控制模板, 不应该在意它是什么 。

ControlTemplate TargetType="Control">
    <Grid Width="444">
        <Grid.RowDefinitions>
            <RowDefinition Height="51" />
            <RowDefinition Height="36" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="#286c97"></Grid>
        <Grid Grid.Row="1" Background="#5898c0">
            <ContentPresenter/>
        </Grid>
    </Grid>
</ControlTemplate>

Contentpresententer Content control 中使用时,与按钮一样,自动附加到模板父体的 Content ContentTemplet ContentTempletSextor 属性。

如果您想要显示的不只是文本, 或想要自定义文本更多, 请直接将 < code> DataTemplate 作为您的 < code> ContentTemplate 直接通过到指定的按钮 。

<DataTemplate x:Key="myButtonContentTemplate">
    <TextBlock FontSize="18" Text="{Binding}"/>
</DataTemplate>

<Button ContentTemplate="{StaticResource myButtonContentTemplate}"/>
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签