北京網(wǎng)站建設(shè)有哪些深圳搜索引擎優(yōu)化推廣
目錄
- 前言
- 一、Style
- 1.1 例子
- 1.2 為樣式起名字
- 1.3 BasedOn 繼承上一個(gè)樣式
- 二、外部Style
- Step1 創(chuàng)建資源字典BaseButtonStyle.xaml
- Step2 在資源字典中寫入Style
- Step3 App.xaml中寫引用路徑【全局】
- Step4 調(diào)用
- 三、代碼提供
- 四、x:Key和x:Name區(qū)別
- 更新時(shí)間
前言
參考文章:
參考視頻:【W(wǎng)PF入門教程 Visual Studio 2022】WPF界面開發(fā)入門
自己的感想
一、Style
如下圖所示,在Windows.Resources中設(shè)置的Style屬性,只在當(dāng)前界面產(chǎn)生效果。
1.1 例子
<Window x:Class="WPF_Study.MainWindow"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"xmlns:local="clr-namespace:WPF_Study"mc:Ignorable="d"Title="WPF入門.txt" Height="600" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="Red"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style></Window.Resources><StackPanel><Button Content="登錄" /><Button Content="幫助" /><Button Content="退出" /></StackPanel></Window>
1.2 為樣式起名字
為啥要起別名?1.1中的案例導(dǎo)致所有的Button組件都是這個(gè)紅色樣式,但是如果我們只想讓退出按鈕為紅色,其他三個(gè)按鈕為綠色,該怎么操作?
<Window x:Class="WPF_Study.MainWindow"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"xmlns:local="clr-namespace:WPF_Study"mc:Ignorable="d"Title="WPF入門.txt" Height="600" Width="800"><Window.Resources><Style x:Key="QuitStyle" TargetType="Button"><Setter Property="Background" Value="Red"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style><Style x:Key="loginStyle" TargetType="Button"><Setter Property="Background" Value="Green"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style></Window.Resources><StackPanel><Button Style="{StaticResource loginStyle}" Content="登錄" /><Button Style="{StaticResource loginStyle}" Content="幫助" /><Button Style="{StaticResource QuitStyle}" Content="退出" /></StackPanel></Window>
1.3 BasedOn 繼承上一個(gè)樣式
<Window x:Class="WPF_Study.MainWindow"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"xmlns:local="clr-namespace:WPF_Study"mc:Ignorable="d"Title="WPF入門.txt" Height="600" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="WhiteSmoke"/><Setter Property="FontSize" Value="20"/><Setter Property="Margin" Value="20,10"/></Style><Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Red"/></Style><Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Green"/></Style></Window.Resources><StackPanel><Button Style="{StaticResource loginStyle}" Content="登錄" /><Button Content="幫助" /><Button Style="{StaticResource QuitStyle}" Content="退出" /></StackPanel></Window>
二、外部Style
需要我們把style放入xaml文件中。
Step1 創(chuàng)建資源字典BaseButtonStyle.xaml
Step2 在資源字典中寫入Style
Step3 App.xaml中寫引用路徑【全局】
Step4 調(diào)用
三、代碼提供
C# WPF中的Style寫入xaml中進(jìn)行全局引用——點(diǎn)擊下載代碼
四、x:Key和x:Name區(qū)別
- 在Resources中進(jìn)行唯一標(biāo)定。
更新時(shí)間
- 2025-02-06:創(chuàng)建。
- 2025-02-07:補(bǔ)充x:key和x:name之間區(qū)別。