WPF - 内容演示者

时间:2015-05-16 15:56:05

标签: wpf

我为Button样式创建了一个资源字典并将其分配给Button。样式出现在按钮中,但我无法显示按钮文本。我尝试过添加内容演示者,但它没有用。请帮忙。

 <Button  x:Name ="Submit" HorizontalAlignment="Left" Margin="346,186,0,0"    
 VerticalAlignment="Top" Width="75" Style="{DynamicResource 
  ResourceKey=ButtonStyle}" Height="50">
       <ContentPresenter Name="MyContent">
            <ContentPresenter.Content>
                <Label>Click Me</Label>
            </ContentPresenter.Content>
        </ContentPresenter>

    </Button>

按钮的样式如下......它是使用Blend ..

创建的
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Rectangle Margin="0,8,0,0" VerticalAlignment="Stretch" Stroke="#FF000000" RadiusX="23.489" RadiusY="23.489">
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF000000" Offset="0"/>
                                <GradientStop Color="#FFDE9090" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="true"/>
                    <Trigger Property="ToggleButton.IsChecked" Value="true"/>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我也试过Button.Content&gt;单击Button.Content&gt;但不工作..请建议

1 个答案:

答案 0 :(得分:0)

样式中的按钮ControlTemplate缺少ContentPresenter来呈现内容。

在按钮的ControlTemplate中添加ContentPresenter。生成的ControlTemplate XAML如下所示:

<ControlTemplate TargetType="{x:Type Button}">
    <Grid>
        <Rectangle Margin="0,8,0,0" VerticalAlignment="Stretch" Stroke="#FF000000" RadiusX="23.489" RadiusY="23.489">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF000000" Offset="0"/>
                    <GradientStop Color="#FFDE9090" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsKeyboardFocused" Value="true"/>
        <Trigger Property="ToggleButton.IsChecked" Value="true"/>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#ADADAD"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

另外,正如我在评论中所说,你不需要XAML for Button本身的ContentPresenter。您可以简单地将您的内容放在标签之间。

  <Button
     x:Name="Submit"
     Width="75"
     Height="50"
     HorizontalAlignment="Left"
     Margin="346,186,0,0"
     VerticalAlignment="Top"
     Style="{DynamicResource    ResourceKey=ButtonStyle}">
        Click Me
  </Button>

有关ControlTemplate的更多信息,请阅读此contenteditable-utils.js页面。