WPF布局常用控件(一)
Border
- Border控件是一個裝飾控件,用于圍繞其他元素繪制邊框和背景。它提供了一種簡單的方式來為其他控件添加邊框和背景樣式,而無需自定義控件的繪制邏輯。
常用屬性 | 描述 |
---|---|
Background | 用于設(shè)置背景顏色或圖像。 |
BorderBrush | 用于設(shè)置邊框的邊框顏色 |
CornerRadius | 用于設(shè)置邊框的圓角程度。 |
BorderThickness | 用于設(shè)置邊框的寬度。它是一個Thickness對象,分別可以設(shè)置上、下、左、右邊框的寬度。通過調(diào)整BorderThickness屬性來控制Border控件邊框的粗細(xì)程度。 |
Padding | 用來設(shè)置內(nèi)邊距 。 |
- 下面寫個例子
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="0" Height="80" BorderBrush="Black" BorderThickness="6" Padding="10" >
<TextBlock Foreground="Red" TextWrapping="Wrap">
邊框顏色(BorderBrush)黑色,邊框線(BorderThickness)同等寬度6
</TextBlock>
</Border>
<Border Grid.Row="1" Grid.Column="0" Height="80" BorderBrush="SlateBlue" BorderThickness="5,10,15,20" Padding="10" CornerRadius="15">
<TextBlock Foreground="Red" TextWrapping="Wrap">
邊框?qū)挾茸笊嫌蚁虏煌?BorderThickness),邊框圓角(CornerRadius)15
</TextBlock>
</Border>
<Border Grid.Row="2" Grid.Column="0" Height="90" BorderBrush="SlateBlue" BorderThickness="5" Background="Green" Padding="10" CornerRadius="15">
<TextBlock Foreground="White" TextWrapping="Wrap">
邊框顏色(BorderBrush)紫色,邊框?qū)挾龋˙orderThickness)上下左右都為5 ,背景顏色(Background)綠色 邊框圓角(CornerRadius)15
</TextBlock>
</Border>
</Grid>
BulletDecorator
- 用于在控件前添加符號(如項目符號)以實現(xiàn)列表項或標(biāo)記樣式的效果。
常用屬性 | 描述 |
---|---|
Bullet | 用于定義顯示在裝飾器前面的“子彈”圖形,這是一個內(nèi)容屬性,可以是任何 UIElement 類型的對象比如一個小圓點、圖像或其他自定義形狀。 |
Child | BulletDecorator 控件的主要內(nèi)容區(qū)域,它可以放置任意類型的 UIElement,例如文本、按鈕、面板等。 |
- 下面寫個例子
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<BulletDecorator Grid.Row="0" Grid.Column="0" Margin="0 20 0 0">
<BulletDecorator.Bullet>
<Ellipse Width="10" Height="10" Fill="Black" />
</BulletDecorator.Bullet>
<BulletDecorator.Child>
<TextBlock Foreground ="Purple">
這是在文本前面增加一個圓點標(biāo)記
</TextBlock>
</BulletDecorator.Child>
</BulletDecorator>
<BulletDecorator Grid.Row="1" Grid.Column="0">
<BulletDecorator.Bullet>
<Image Width="10" Height="10" Source="1.png" />
</BulletDecorator.Bullet>
<BulletDecorator.Child>
<TextBlock Foreground ="Purple">
這是在文本前面增加一個圖片
</TextBlock>
</BulletDecorator.Child>
</BulletDecorator>
</Grid>
Button
- 用于觸發(fā)一個操作或事件。Button 控件允許用戶通過單擊或觸摸來與應(yīng)用程序進(jìn)行交互。
常用屬性 | 描述 |
---|---|
Content | 用于設(shè)置按鈕上顯示的文本或內(nèi)容。 |
Background | 設(shè)置按鈕的背景顏色或圖像。 |
Foreground | 設(shè)置按鈕上文本的顏色。 |
Width | 設(shè)置按鈕的寬度。 |
Height | 設(shè)置按鈕的高度。 |
BorderBrush | 設(shè)置按鈕邊框的顏色。 |
BorderThickness | 設(shè)置按鈕邊框的寬度。 |
Padding | 設(shè)置按鈕內(nèi)容與邊框之間的空間。 |
FontFamily, FontSize, FontStyle, FontWeight | 用于設(shè)置按鈕上文本的字體屬性。 |
IsEnabled | 用于啟用或禁用按鈕的交互功能。 |
Click | 綁定到按鈕的 Click 事件處理程序。 |
Template | 用于自定義按鈕的外觀和布局。 |
- 下面寫個列子
<Window.Resources>
<ControlTemplate x:Key="MyButtonTemplate" TargetType="Button">
<BulletDecorator VerticalAlignment="Center">
<BulletDecorator.Bullet>
<Ellipse Fill="LightGray" Stroke="Red" StrokeThickness="1" Width="20" Height="20"/>
</BulletDecorator.Bullet>
<TextBlock Text="{TemplateBinding Content}" Background="Aquamarine" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</BulletDecorator>
</ControlTemplate>
</Window.Resources>
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Height="40" Width="200" Content="設(shè)置字體顏色" Foreground="Blue" ></Button>
<Button Grid.Column="0" Grid.Row="1" Height="40" Width="200" BorderBrush="Black" Content="設(shè)置邊框顏色"></Button>
<Button Grid.Column="0" Grid.Row="2" Height="40" Width="200" BorderBrush="AntiqueWhite" BorderThickness="6" Content="設(shè)置邊框顏色"></Button>
<Button Grid.Column="0" Grid.Row="3" Height="40" Width="200" Background="Green" Foreground="White" Content="設(shè)置背景顏色"></Button>
<Button Grid.Column="0" Grid.Row="4" Height="40" Width="200" Content="我被禁用了" IsEnabled="False" ></Button>
<Button Grid.Column="0" Grid.Row="5" Height="40" Width="200" Content="我綁定了點擊事件" Click="Button_Click" ></Button>
<Button Grid.Column="0" Grid.Row="6" Height="40" Width="200" FontFamily="Gabriola" FontWeight="Bold" FontSize="20" FontStyle="Italic" Content="設(shè)置了字體相關(guān)屬性"></Button>
<Button Grid.Column="0" Grid.Row="7" Template="{StaticResource MyButtonTemplate}" Content="我使用了模板"></Button>
</Grid>
文章來源:http://www.zghlxwxcb.cn/news/detail-810499.html
公眾號“點滴分享技術(shù)猿”
文章來源地址http://www.zghlxwxcb.cn/news/detail-810499.html
到了這里,關(guān)于掌握WPF控件:熟練常用屬性(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!