繁体   English   中英

WPF创建带有自定义内容模板的按钮

[英]WPF create button with custom content template

我在WPF中有一个应用程序,需要在其中创建具有相同内容布局的多个按钮。 当前在Window中定义为:

<Button Grid.Row="0" Grid.Column="0" Margin="4" >
    <Button.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.85*" />
                <RowDefinition Height="0.25*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" TextAlignment="Center" Text="Primary Text that can wrap" TextWrapping="Wrap" FontSize="14.667" />
            <TextBlock Grid.Row="1" TextAlignment="Left" Text="smaller text" FontSize="10.667" />
        </Grid>
    </Button.Content>
</Button>

我理想的情况是将其更改为:

<controls:MultiTextButton Grid.Row="0" Grid.Column="0" PrimaryText="Primary Text that can wrap" SecondaryText="smaller text" />

我正确或错误地创建了以下类:

public class MultiTextButton : Button
{
    public static readonly DependencyProperty PrimaryTextProperty = DependencyProperty.Register("PrimaryText", typeof(String), typeof(MultiTextButton));

    public static readonly DependencyProperty SecondaryTextProperty = DependencyProperty.Register("SecondaryText", typeof(String), typeof(MultiTextButton));

    static MultiTextButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiTextButton), new FrameworkPropertyMetadata(typeof(MultiTextButton)));
    }

    public string PrimaryText
    {
        get { return (string)GetValue(PrimaryTextProperty); }
        set { SetValue(PrimaryTextProperty, value); }
    }

    public string SecondaryText
    {
        get { return (string)GetValue(SecondaryTextProperty); }
        set { SetValue(SecondaryTextProperty, value); }
    }
}

我现在不确定如何设置“模板”以在窗口中以原始代码的格式显示内容。 我试过了:

<ControlTemplate x:Key="MultiTextButtonTemplate" TargetType="{x:Type controls:MultiTextButton}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.85*" />
            <RowDefinition Height="0.25*" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
        <TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />

    </Grid>
</ControlTemplate>

但是在Blend和Visual Studio中,按钮未呈现。

您正在使用TemplateBinding

<TextBlock Grid.Row="0" TextAlignment="Center" Text="{TemplateBinding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="{TemplateBinding SecondaryText}" FontSize="10.667" />

此绑定专门用于控件模板-引用控件中的依赖项属性。 (相反,请注意,常规绑定引用当前DataContext的属性。)


编辑

为了使其看起来像一个按钮,请复制Button默认控件模板,并将其ContentPresenter替换为以下内容:

<ContentControl Margin="2"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                RecognizesAccessKey="True"
                ContentTemplate="{TemplateBinding ContentTemplate}">
    <ContentControl.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.85*" />
                <RowDefinition Height="0.25*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
            <TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />

        </Grid>
    </ContentControl.Content>
</ContentControl>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM