我在WPF window里放了两个border,我想在点击button时,border的borderthickness改变,给我一段完整示例代码
时间: 2024-02-26 13:56:47 浏览: 214
WPF示例代码
好的,以下是一个简单的示例代码,点击按钮时可以改变两个Border的BorderThickness属性:
XAML代码:
```
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="btnChangeBorderThickness" Content="Change Border Thickness" Click="btnChangeBorderThickness_Click"/>
</StackPanel>
<Grid Grid.Row="1" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="border1" BorderThickness="1" BorderBrush="Black" Margin="5">
<TextBlock Text="Border 1"/>
</Border>
<Border x:Name="border2" BorderThickness="1" BorderBrush="Black" Grid.Column="1" Margin="5">
<TextBlock Text="Border 2"/>
</Border>
</Grid>
</Grid>
</Window>
```
C#代码:
```
using System.Windows;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnChangeBorderThickness_Click(object sender, RoutedEventArgs e)
{
// 改变Border1的BorderThickness
if (border1.BorderThickness.Equals(new Thickness(1)))
{
border1.BorderThickness = new Thickness(5);
}
else
{
border1.BorderThickness = new Thickness(1);
}
// 改变Border2的BorderThickness
if (border2.BorderThickness.Equals(new Thickness(1)))
{
border2.BorderThickness = new Thickness(5);
}
else
{
border2.BorderThickness = new Thickness(1);
}
}
}
}
```
在这个示例中,我们在Window中放置了一个StackPanel和一个Grid,StackPanel中有一个Button,Grid中有两个Border。当点击Button时,C#代码会检查两个Border的BorderThickness属性,如果当前值为1,则将其改为5,否则将其改为1。这个操作会实现一个简单的切换效果。
阅读全文