WPF中用xaml怎么给Gird或canvas插入背景图片?
1个回答
展开全部
下面我就大概说下过程,首页建立一个工程为WpfLoginView,并在Expression Blend 下设置一个如下图的界面
xaml代码如下:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="WpfLoginView.LoginView"
x:Name="Window"
Title="LoginView" mc:Ignorable="d" KeyDown="Window_KeyDown" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" AllowsTransparency="True" WindowStyle="None" Foreground="#FF9DC2EF">
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120.012"/>
<ColumnDefinition Width="209.988"/>
<ColumnDefinition Width="81"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="72.899"/>
<RowDefinition Height="30.101"/>
<RowDefinition Height="43"/>
<RowDefinition Height="43"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<Label HorizontalAlignment="Left" Content="用户名" Margin="0,8" Grid.Column="1" Grid.Row="2" Width="60" FontSize="16" Foreground="#FFE2E4EB"/>
<Label HorizontalAlignment="Left" Margin="0,4,0,12" Content="密 码" Grid.Column="1" Grid.Row="3" FontSize="16" Width="64" Foreground="#FFEEF0F5"/>
<TextBox x:Name="txtusername" TextWrapping="Wrap" Margin="0,8,8,10" Grid.Column="1" Grid.Row="2" BorderThickness="2" HorizontalAlignment="Right" Width="137.988" d:LayoutOverrides="GridBox">
<TextBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="#FFBCDBF9" Offset="1"/>
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
<PasswordBox x:Name="txtpassword" Margin="0,6,8,11" Grid.Column="1" Grid.Row="3" BorderThickness="2" HorizontalAlignment="Right" Width="137.988" d:LayoutOverrides="GridBox">
<PasswordBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="#FFC1DBF5" Offset="1"/>
</LinearGradientBrush>
</PasswordBox.BorderBrush>
</PasswordBox>
<Button x:Name="LoginConfirmButton" Content="登 陆" Grid.Column="1" Grid.Row="4" Click="LoginConfirmButton_Click" FontSize="16" VerticalAlignment="Top" Height="24.687" Margin="64,8,91.988,0" Style="{DynamicResource ConfirmCancelButtonStyle}" />
<Button x:Name="LoginCancelButton" Margin="144.988,8,0,0" Content="取 消" HorizontalAlignment="Left" Width="57" Grid.Column="1" Grid.Row="4" Click="LoginCancelButton_Click" FontSize="16" VerticalAlignment="Top" Height="24.687" Style="{DynamicResource ConfirmCancelButtonStyle}"/>
</Grid>
</Window>
注意一下这几个属性的设置:WindowStartupLocation="CenterScreen" AllowsTransparency="True" WindowStyle="None"。两个Button的样式代码我就不贴了,下面我们假如有下面这么一张图片(Login.png),如何成为背景呢?
首先把这张图放在工程中
public LoginView()
{
this.InitializeComponent();
txtusername.Focus();//聚焦在用户名输入框中
// 在此点之下插入创建对象所需的代码。
ImageBrush b = new ImageBrush();
b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Login.png"));
b.Stretch = Stretch.Fill;
this.Background = b;
}
直接在构造函数中输入以上代码就Ok了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询