按钮内容在WPF中不可见

问题描述:

我正在使用Static Resource设计WPF表单。我正在使用以下代码来设计登录表单。按钮内容在WPF中不可见

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="WpfApplication1.Login" 
x:Name="Window" 
Title="IntelCall | Login" 
Width="640" Height="450" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" WindowStyle="None"> 
<Window.Background> 
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="Black" Offset="0"/> 
     <GradientStop Color="White" Offset="1"/> 
    </LinearGradientBrush> 
</Window.Background> 
<Window.Resources> 

    <Style x:Key="DefaultButton" TargetType="Button"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="FontSize" Value="16px"/> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="Width" Value="125"/> 
     <Setter Property="Background" > 
      <Setter.Value> 
       <LinearGradientBrush EndPoint="0.5,4.201" StartPoint="0.5,-3.792"> 
        <GradientStop Color="#FF001D4F"/> 
        <GradientStop Color="White" Offset="1"/> 
        <GradientStop Color="#FF001D4F" Offset="0.515"/> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Name="border" 
        BorderThickness="2"    
        BorderBrush="White"  
        CornerRadius="5"    
        Background="{TemplateBinding Background}"> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid x:Name="LayoutRoot"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="640"/> 
    </Grid.ColumnDefinitions> 
    <Grid.Background> 
     <LinearGradientBrush EndPoint="0.495,1.258" StartPoint="0.496,-0.049"> 
      <GradientStop Color="#FF001D4F" Offset="0.249"/> 
      <GradientStop Color="#FF5888DA" Offset="1"/> 
      <GradientStop Color="#FF073A93" Offset="0.838"/> 
     </LinearGradientBrush> 
    </Grid.Background> 
    <Rectangle HorizontalAlignment="Stretch" Height="44.734" Margin="0,405.766,0,-0.5" Stroke="Black" StrokeThickness="0" VerticalAlignment="Top" Grid.ColumnSpan="3"> 
     <Rectangle.Fill> 
      <LinearGradientBrush EndPoint="0.901,0.84" StartPoint="-0.164,0.84"> 
       <GradientStop Color="#FF001D4F" Offset="0.22"/> 
       <GradientStop Color="#FF001D4F" Offset="0.878"/> 
      </LinearGradientBrush> 
     </Rectangle.Fill> 
    </Rectangle> 

    <TextBox HorizontalAlignment="Left" Height="27.074" Margin="356.049,164.54,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="264.152" Background="Transparent" BorderBrush="White" BorderThickness="2" Foreground="White" Padding="3"/> 
    <TextBox HorizontalAlignment="Left" Height="27.074" Margin="356.049,224.54,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="264.152" Background="Transparent" BorderBrush="White" BorderThickness="2" Foreground="White" Padding="3"/> 
    <Label x:Name="label_Copy" Content="Login ID" HorizontalAlignment="Left" Margin="200.978,158.154,0,0" VerticalAlignment="Top" FontSize="18.667" Foreground="White" FontFamily="Microsoft MHei" RenderTransformOrigin="0.694,0.477" Height="33.46" Width="93.723"> 
     <Label.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </Label.RenderTransform> 
    </Label> 
    <Label x:Name="label_Copy1" Content="Login Password" HorizontalAlignment="Left" Margin="200.978,218.154,0,0" VerticalAlignment="Top" FontSize="18.667" Foreground="White" FontFamily="Microsoft MHei" RenderTransformOrigin="0.694,0.477" Height="33.46" Width="146.223"> 
     <Label.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </Label.RenderTransform> 
    </Label> 
    <Button Style="{StaticResource DefaultButton}" x:Name="BtnLogin" Content="Login" HorizontalAlignment="Left" Margin="356.049,279,0,0" VerticalAlignment="Top" Width="125" Height="30.46"> 

    </Button> 
    <Button Style="{StaticResource DefaultButton}" x:Name="BtnReset" HorizontalAlignment="Left" Margin="495.201,279,0,0" VerticalAlignment="Top" Height="30.46" FontFamily="/WpfApplication1;component/Fonts/#Segoe UI"> 
     Reset 
    </Button> 
    <Image Margin="37.511,161.585,431.034,116.96" Source="Image/Phone.png" RenderTransformOrigin="0.5,0.5"> 
     <Image.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform Angle="-82.577"/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </Image.RenderTransform> 
    </Image> 
    <Label Content="Intel" HorizontalAlignment="Left" Margin="210.274,10,0,0" VerticalAlignment="Top" FontSize="48" Foreground="White" FontFamily="Microsoft MHei"/> 
    <Label Content="Call" HorizontalAlignment="Left" Margin="309.171,-4.444,0,0" VerticalAlignment="Top" FontSize="48" Foreground="White" FontFamily="Kozuka Gothic Pro B" FontWeight="Bold"/> 
    <Label Content="v1.0" HorizontalAlignment="Left" Margin="401.201,27.266,0,0" VerticalAlignment="Top" FontSize="32" Foreground="White" FontFamily="Microsoft MHei"/> 
</Grid> 

我的问题是,按钮内容 “复位”, “登陆” 是不可见的。 如果我删除静态资源及给所有的属性按钮标签,按钮的内容完美的作品

谢谢你的帮助,下面是输出 http://prntscr.com/70lccv

网址在你ControlTemplate你应该有一个ContentPresenter显示内容为Button

当您定义ControlTemplate时,您还应该定义如何显示按钮的内容。您可以使用ContentPresenter来显示内容。您也可以使用TextBoxTextBlockLabel等来显示按钮的内容。

请参阅下面的代码。

<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="Button"> 
      <Border Name="border" 
        BorderThickness="2"    
        BorderBrush="White"  
        CornerRadius="5"    
        Background="{TemplateBinding Background}"> 
       <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 
+0

好的。谢谢你murali。它正在工作。我很快就会接受你的回答。 –

+0

你能帮我一点点吗?为什么ContentPresenter需要在这里? –