2

我在 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 中,该按钮未呈现。

4

1 回答 1

3

你在追求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>
于 2013-10-22T00:04:14.073 回答