找到你要的答案

Q:Button content not visible in WPF

Q:在WPF按钮不可见的内容

I am designing WPF form using Static Resource. I am using following code to design log in form.

<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>

My problem is, Button Content "Reset" and "Login" is not visible. If I remove Static Resourse and gives all the property in button tag, button content works perfectly

Thank You for Helping, Here is URL for Output http://prntscr.com/70lccv

我使用静态资源设计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>

My problem is, Button Content "Reset" and "Login" is not visible. If I remove Static Resourse and gives all the property in button tag, button content works perfectly

Thank You for Helping, Here is URL for Output http://prntscr.com/70lccv

answer1: 回答1:

In your ControlTemplate you should have a ContentPresenter to display the content of the Button.

When you define the ControlTemplate, you should also define how the content of the button has to be displayed. You can use aContentPresenter to display the content. You can also use a TextBox or TextBlock or Label etc to display the content of the button.

See below code.

<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>

你必须要有一个contentpresenter controltemplate显示按钮的内容。

当你定义controltemplate,你也应该如何定义按钮的内容已经被显示。你可以使用acontentpresenter显示内容。你也可以使用文本框或TextBlock或标签等,显示该按钮的内容。

见下面代码。

<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>
c#  wpf