【c#】Border

一、基本样式设置

1、基本样式设置

BorderThickness、Margin、Padding值类型均是Thickness

<border>
BorderThickness="1"
BorderBrush = "#076ff5"
Background=""
Foreground=""
Margin=""
Padding=""
CornerRadius=""<!-- 圆角 -->
></border>

二、相关样式示例

1、开关

<Button Click="Button_ChangeTraceStatus">
   <Button.Template>
        <ControlTemplate>
            <Border x:Name="switchBor" Background="#E6E6E6"  CornerRadius="8" Width="30" Height="16">
                <Grid>
                    <Ellipse x:Name="spSwitchOff" HorizontalAlignment="Left" Margin="2,0" Width="12" Height="12" Fill="#FFF"/>
                    <Ellipse x:Name="spSwitchOn" HorizontalAlignment="Right" Margin="0,0,2,0" Width="12" Height="12" Fill="#FFF" Visibility="Collapsed"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding TraceTableStatus}" Value="1">
                    <Setter Property="Visibility" TargetName="spSwitchOff" Value="Collapsed"></Setter>
                    <Setter Property="Visibility" TargetName="spSwitchOn" Value="Visible"></Setter>
                    <Setter Property="Background" TargetName="switchBor" Value="#076ff5"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding TraceTableStatus}" Value="0">
                    <Setter Property="Visibility" TargetName="spSwitchOff" Value="Visible"></Setter>
                    <Setter Property="Visibility" TargetName="spSwitchOn" Value="Collapsed"></Setter>
                    <Setter Property="Background" TargetName="switchBor" Value="#E6E6E6"></Setter>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

在这里插入图片描述

2、虚线 border

<Border Width="100" Height="100" CornerRadius="50" BorderThickness="1">
    <Border.BorderBrush>
        <VisualBrush>
            <VisualBrush.Visual>
            	<!-- 使用Rectangle则是矩形边框,StrokeDashArray设置的虚线/点的密度  -->
                <Ellipse StrokeDashArray="2 2" Stroke="#cccccc" StrokeThickness="1"
                    Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
                    Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Border.BorderBrush>
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#cccccc" FontSize="40">+</TextBlock>
</Border>

在这里插入图片描述

C# 中,`Border` 控件的 `ToolTip` 属性用于设置当鼠标悬停在其上的提示信息。以下是有关该属性的一些详细说明以及如何使用的示例。 ### 设置 Border 的 ToolTip 属性 `ToolTip` 是一种常见的 UI 功能,在 WPF 和 WinForms 开发中被广泛使用。对于 WPF 应用程序中的 `Border` 控件来说,可以通过直接绑定其 `ToolTip` 属性来实现功能[^2]。 #### 示例代码 下面是一个简单的例子,展示如何在 WPF 中为 `Border` 添加工具提示: ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Border Tooltip Example" Height="200" Width="300"> <Grid> <!-- 定义一个带有 ToolTip 的 Border --> <Border Background="LightGray" BorderBrush="Black" BorderThickness="1" CornerRadius="5" Margin="10"> <Border.ToolTip> <ToolTip>This is a tooltip for the border.</ToolTip> </Border.ToolTip> <TextBlock Text="Hover over me!" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </Grid> </Window> ``` 在这个 XAML 文件中,定义了一个具有灰色背景和黑色边框的区域 (`Border`) 并设置了它的 `ToolTip` 属性。当用户将光标移动到此边界上时,会显示指定的消息:“This is a tooltip for the border.”[^2] 如果是在 Windows Forms 环境下,则需要借助于单独创建的一个 `Tooltip` 对象实例并将其关联至目标控件上来完成同样的效果[^3]: ```csharp using System; using System.Windows.Forms; public class FormWithTooltip : Form { public FormWithTooltip() { // 创建一个新的 Label 或其他类型的控件作为演示对象 var label = new Label { Text = "Hover Over Me!", AutoSize = true }; Controls.Add(label); // 初始化 ToolTip 组件 var toolTip = new ToolTip(); toolTip.SetToolTip(label, "This is an example of using tooltips in WinForms."); } [STAThread] static void Main(){ Application.EnableVisualStyles(); Application.Run(new FormWithTooltip()); } } ``` 以上两段分别适用于不同框架下的解决方案展示了如何配置相应的控件以支持悬浮气泡帮助文字的功能[^3]. ### 注意事项 虽然上述方法可以正常工作,但在实际项目开发过程中还需要考虑更多细节比如国际化处理、动态更新内容等场景需求[^4]. 此外需要注意的是并非所有的容器类组件都原生支持这种交互行为所以有时可能需要用到自绘技术或者第三方库扩展原有能力范围之外的新特性[^5].
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值