【问题标题】:WPF Button Style Doesn't Show TextWPF 按钮样式不显示文本
【发布时间】:2012-04-23 06:00:47
【问题描述】:

我是新手,所以请多多包涵。

我现在有这个Style。将其粘贴到窗口中以查看它的运行情况。

<Window.Resources>

<Style x:Key="SelectionButton3"
        TargetType="{x:Type Button}">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        ClipToBounds="False">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="75" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="55" />
                    </Grid.ColumnDefinitions>

                    <Border x:Name="TheBorder"
                            BorderThickness="0,1.5,1.5,1.5"
                            CornerRadius="3"
                            Background="SteelBlue"
                            Height="35"
                            Grid.Column="1"
                            Grid.Row="0"
                            Margin="-31"
                            BorderBrush="DarkSlateBlue"/>

                    <Rectangle Name="TheRectangle"
                                Fill="SteelBlue"
                                Stroke="DarkSlateBlue"
                                Grid.Row="0"
                                Grid.Column="0">

                        <Rectangle.LayoutTransform>
                            <RotateTransform Angle="-45" />
                        </Rectangle.LayoutTransform>

                        <Rectangle.BitmapEffect>
                            <DropShadowBitmapEffect ShadowDepth="5" />
                        </Rectangle.BitmapEffect>

                    </Rectangle>

                    <ContentPresenter x:Name="ContentArea"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Center"
                                       Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" />
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                                Value="True">
                        <Setter TargetName="TheBorder"
                                Property="BitmapEffect">
                            <Setter.Value>
                                <DropShadowBitmapEffect ShadowDepth="0" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>

    </Setter>

    <Setter Property="Foreground"
            Value="White"/>
    <Setter Property="FontFamily"
            Value="Segoe UI" />
    <Setter Property="FontSize"
            Value="20" />

    </Style>

<Grid.RowDefinitions>
    <RowDefinition Height="20"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="20"/>
    <ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>

<Button Grid.Row="1"
        Grid.Column="1"
        Style="{StaticResource SelectionButton3}"
        Margin="0,0,0,5"
        Click="Button_Click">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Image Height="35"
               Width="35"
               Stretch="Fill"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               Grid.Column="0"
               Margin="32.5,0,0,0"
               Source="/app;component/Media/Images/Mail/email.png" />

        <TextBlock Text="Email"
                   Grid.Column="1"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Margin="22,0,0,0"/>

    </Grid>


</Button>

我有两个问题:

1) 为什么只显示部分文字?

2) 如何将ImageTextBlock 放入Template 并绑定到它们?

【问题讨论】:

    标签: wpf button styles controltemplate


    【解决方案1】:

    您的文本被剪裁的原因是您没有为ContentPresenter 指定 Grid.Row 和 Grid.Column。这会导致您的按钮内容默认为ControlTemplate 上的第 0 行和第 0 列,这与您的边距和位置相结合会导致按钮元素上的文本块被切断。我猜这可能是你的意图?

    <ContentPresenter x:Name="ContentArea"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Grid.Row="0"
       VerticalAlignment="Stretch"
       HorizontalAlignment="Stretch"
       Content="{Binding Path=Content, 
                 RelativeSource={RelativeSourceTemplatedParent}}" />
    

    现在ContentPresenter 将跨越ControlTemplate 网格上的两列,因此您的文本块将有更多空间显示自己。

    回答你的第二个问题How do I put the Image and Textblock in the template and just bind to them?

    一种方法是继承Button 类,例如将其称为MyImageButton,并在这个新类中实现两个新的依赖属性ImageText。然后您可以将您的&lt;Image&gt;&lt;TextBlock&gt; 移动到您的控制模板中,并将它们绑定到您的新ImageText 属性。然后您可以实例化MyImageButton 并使用新的属性来设置按钮的图像和文本。

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多