当前位置: 首页 > news >正文

XAML笔记

HorizontalAlignment="Center"                //水平方向的居中对齐

VerticalAlignment="Center"                    //竖直方向的居中对齐

xml:space="preserve"                            //保留原格式(不添加这个属性想显示多空格时只会显示单空格)

Margin="10,10,10,10"                            //边框

Orientation="Horizontal"                        //用于容器使标签水平排布

事件

要使用控件的时候别忘了给控件起名

x:Name=""

ValueChanged      //值改变触发事件    Slider

TextChanged        //数据改变触发事件   Text

绑定

在触发事件中把各个控件和函数建立关系太过于麻烦,可以尝试使用绑定的方式。

控件与控件的绑定

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
   
       
    
    <StackPanel>
        <Button Width="100" Height="40" />
        <Slider x:Name="slider"/>
        <TextBox  Height="40" Text="{Binding ElementName=slider,//要绑定的控件名
                                     Path=Value,                //要绑定控件的部位
                                     Mode=TwoWay             //绑定模式:
                                     }"/>                     TwoWay 双向绑定  
                                                              OneWay 单向绑定
                                                      OneWayToSource 相反的单向绑定
                                                              OneTime 绑定的第一次 
    </StackPanel>
</Window>

控件和属性的绑定

1、创建一个类

internal class Students
{
    private string _name;
    public string Name { get => _name; set => _name = value; }
}

2、在MainWindow中给属性赋给面板

public partial class MainWindow : Window
{
    MainWindow()
    {
        InitializeComponent();
        this.DataContext = new Students() { Name="文豪" };
    }
}

3、xaml

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">



    <StackPanel>
        <TextBox  Height="40" Text="{Binding Name}" TextChanged="TextBox_TextChanged"/>
    </StackPanel>
</Window>

样式

Style的作用是给控件属性重用,当很多组件的属性一致时,只需要用同一个样式就可以了

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    //TargetType   要给控件的类型
    //x:Key        要添加样式的控件绑定这个key,如果不写这个key默认全局butten都使用这个样式
    //样式之间可以相互继承
    <Window.Resources>
         <Style TargetType="Button" x:Key="BaseButtonStyle">
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Background" Value="Black"/>
        </Style>
        <Style TargetType="Button" x:Key="ButtonStyle" BasedOn="{StaticResource BaseButtonStyle}">
            <Setter Property="Content" Value="Butten"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Style="{StaticResource ButtonStyle}"></Button>
        <Button Style="{StaticResource ButtonStyle}"></Button>
        <Button Style="{StaticResource ButtonStyle}"></Button>
        <Button Style="{StaticResource ButtonStyle}"></Button>
        <Button Style="{StaticResource ButtonStyle}"></Button>
        <Button Style="{StaticResource ButtonStyle}"></Button>

    </StackPanel>
</Window>

容器

Border:经常配合容器一起使用,作用是给元素添加边框

<Window x:Class="Wpf_Panel.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_Panel"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="800">
    <StackPanel>
        <!--BorderBrush用来设置颜色-->
        <!--BorderThickness用来设置宽度-->
        <!--CornerRadius设置圆角-->
        <!--Padding设置边框和里面元素的间距-->
        <!--Margin设置边框和其他临近元素的间距-->
        <!--Background则是设置border所有内容的颜色-->
        <Border Background="Bisque" BorderBrush="Coral"  BorderThickness="3">
            <Button Content="Button A" Width="120"/>
        </Border>
        <Border BorderBrush="Red" BorderThickness="3" Margin="5">
            <Button Content="Button B"/>
        </Border>
        <Border BorderBrush="DarkRed" BorderThickness="3" Background="Red" Padding="5">
            <Button Content="Button C"/>
        </Border>
    </StackPanel>
    <!--<Grid>
         
    </Grid>-->
</Window>

Grid: 网格面板,是以网状表格的形式对元素进行布局,控件被放到设定好的小格子里面,每个小格子可以按照自己设定的比例进行全局缩放,对于制作自适应界面来说比较方便。

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
   
    <Grid >
        //三种布局方式 1、自适应布局 2、绝对布局 3、按比例布局
        //Grid.Row:当前控件所在行
        //Grid.Column:当前控件所在列
        //Grid.ColumnSpan:元素跨列,自Grid.Column起的几格
        //Grid.RowSpan:元素跨行,自Grid.Row起的几格
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="180"/>
        </Grid.RowDefinitions >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <Border Background="BlueViolet" Grid.Row="0" Grid.Column="0">
            <Button Content="123" Height="50" Width="100"></Button>
        </Border>
        <Border Background="Gainsboro" Grid.Row="0" Grid.Column="1"></Border>
        <Border Background="Brown" Grid.Row="1" Grid.Column="0"></Border>
        <Border Background="Green" Grid.Row="1" Grid.Column="1"></Border>
        <Border Background="Beige"  Grid.Row="2" Grid.Column="0"   Grid.ColumnSpan="2">
        </Border>

    </Grid>
</Window>

StackPanel:堆栈面板,水平或垂直放置元素(如果元素过多摆不下就不会显示)

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
        
        //Orientation="Horizontal" 水平排布
        //Orientation="Vertical"  竖直排布
    <StackPanel Orientation="Horizontal">
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
        <Button Width="100" Height="40"></Button>
    </StackPanel>
</Window>

WrapPanel:可换行的行中放置元素(就是StackPanel 的自动换行版,摆不下的元素不会不显示而是自动换行),在水平方向上从左向右放置元素,换行后也是从左向右。在垂直方向上,从上到下放置元素,在切换列后也是从上到下。

DockPanel: 停靠面板,根据容器的整个边界调整元素。

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
        //属性
        //LastChildFill="False" 如果值为true,最后一个控件会充满容器剩下的空间
        //DockPanel.Dock  空间在哪个方向停靠
    <DockPanel LastChildFill="False">
        <Button Width="100" Height="40" DockPanel.Dock="Left"></Button>
        <Button Width="100" Height="40" DockPanel.Dock="Right"></Button>
        <Button Width="100" Height="40" DockPanel.Dock="Bottom"></Button>
        <Button Width="100" Height="40" DockPanel.Dock="Top"></Button>
        <Button Width="100" Height="40"></Button>

    </DockPanel>
</Window>

UniformGrid:均分的网格面板。

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    //属性 
    //Columns   分为多少行
    //Rows      分为多少列
    <UniformGrid Columns="3" Rows="3"> 
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
        <Button></Button>
       
        
    </UniformGrid>
</Window>

Canvas:使用固定坐标绝对定位元素。

ScrollViewer:通过添加滚动条可以使当前过长布局内的内容纵向或者横向滚动。再有限的区域内可以通过滚动呈现更多的内容。

相关文章:

  • 5个自动化小技巧:聪明的员工和管理者如何提升效率?
  • JAVA轻量级错误码设计最佳实践
  • Nidia的Deepstream,官方给的案例介绍,deepstream-test1\deepstream-tes2\deepstream-test5
  • RESTful风格学习笔记【包含示例】
  • Java:单例模式详解
  • 第十九天人工智能课程总结
  • opencv入门 二
  • Java:在Java中使用函数接口
  • 百度小程序SEO指南
  • 【C++提高】一篇文章了解 C++ 操作 TDengine(详解)
  • CSDN客诉周报第8期|本周解决15个用户问题
  • 小白学习-ElasticSearch教程(2) -文档查询之match查询 | 分词器
  • 中移链DDC-SDK技术对接全流程(一)
  • 苹果应用如何在windows上架应用?
  • 阿里业务中台到底是什么样子
  • 【391天】每日项目总结系列128(2018.03.03)
  • iOS小技巧之UIImagePickerController实现头像选择
  • Java基本数据类型之Number
  • mongodb--安装和初步使用教程
  • Promise面试题2实现异步串行执行
  • Python 反序列化安全问题(二)
  • 阿里云前端周刊 - 第 26 期
  • 高度不固定时垂直居中
  • 前嗅ForeSpider教程:创建模板
  • 软件开发学习的5大技巧,你知道吗?
  • 一个JAVA程序员成长之路分享
  • 一些关于Rust在2019年的思考
  • 最简单的无缝轮播
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 容器镜像
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • (06)Hive——正则表达式
  • (09)Hive——CTE 公共表达式
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • .NET MVC之AOP
  • .net refrector
  • .NET使用存储过程实现对数据库的增删改查
  • .net下简单快捷的数值高低位切换
  • @Transaction注解失效的几种场景(附有示例代码)
  • [20150904]exp slow.txt
  • [2016.7.Test1] T1 三进制异或
  • [2024] 十大免费电脑数据恢复软件——轻松恢复电脑上已删除文件
  • [ABC294Ex] K-Coloring
  • [AIGC] Spring Interceptor 拦截器详解
  • [c++] 自写 MyString 类
  • [CSS]中子元素在父元素中居中
  • [InnoDB系列] -- SHOW INNODB STATUS 探秘
  • [ISITDTU 2019]EasyPHP
  • [JavaEE系列] Thread类的基本用法