C# WPF布局

news/2024/9/23 21:07:34/

布局:

1、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 Margin="0,0,12,0">

     <!--布局容器-->

        <Grid.RowDefinitions>

     <!--定义它的行以及它的高度-->

            <RowDefinition Height="40"></RowDefinition>

            <RowDefinition Height="Auto"></RowDefinition>

            <RowDefinition Height="2*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

     <!--定义它的列以及它的宽度-->

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

        </Grid.ColumnDefinitions>

   

        <Button Grid.Row="0" Grid.Column="2" Content="button1"></Button>

     <!--第0行第二列-->

        <Button Grid.Row="0" Grid.Column="1" Content="button3"></Button>

     <!--第0行第1列-->

        <Button Grid.Row="1" Content="button2"></Button>

     <!--第一行-->

        <Button Grid.Row="2" Content="button4"></Button>

     <!--//第二行-->

        <Button Grid.Row="3" Content="button5"></Button>

     <!--//第三行-->

    </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">

   2、 <Grid>

        <StackPanel  Name="Stcak1" Orientation="Horizontal">

            <Button Content="button1"/>

            <Button Content="button2"/>

        </StackPanel>

        <StackPanel x:Name="Stack2" Orientation="Vertical">

            <Button Content="button3"></Button>

            <Button Content="button4"></Button>

            <Button Content="button5"></Button>

        </StackPanel>

        <StackPanel Name="stack3" Orientation="Horizontal" FlowDirection="RightToLeft">

            <Button Content="button6"></Button>

            <Button Content="button7"></Button>

        </StackPanel>

    </Grid>

3、WrapPanel://自动换行环列

<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">

    <WrapPanel Orientation="Horizontal">

        <Button Content="button 150" Width="150"></Button>

        <Button Content="button 200" Width="200"></Button>

        <Button Content="button 150" Width="150"></Button>

        <Button Content="button 200" Width="200"></Button>

        <Button Content="button 150" Width="150"></Button>

    </WrapPanel>

</Window>

DockPanel:

    <DockPanel>

        <Button Content="左"  DockPanel.Dock="Left"></Button>

        <Button Content="下"  DockPanel.Dock="Bottom" ></Button>

        <Button Content="右"  DockPanel.Dock="Right"></Button>

        <Button Content="上"  DockPanel.Dock="Top" ></Button>

    </DockPanel>

4、UniformGrid://按照输入顺序排列到容器当中

    <UniformGrid >

        <Button Content="Button"></Button>

        <Button Content="Button1"></Button>

        <Button Content="Button2"></Button>

        <Button Content="Button3"></Button>

        <Button Content="Button4"></Button>

    </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">

    <Grid>

        <Canvas>

            <Button   Content="Button1" Canvas.Left="50"  Canvas.Top="50"></Button>

            <Button   Content="Button2" Canvas.Right="50" Canvas.Top="50" ></Button>

            <Button   Content="Button3" Canvas.Left="50"  Canvas.Bottom="50" ></Button>

            <Button   Content="Button3" Canvas.Left="50"  Canvas.Bottom="50" ></Button>

        </Canvas>

    </Grid>

</Window>

ScrollViewer:滑动框

    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">

        <Button Content="Button" Width="800" Height="800"></Button>

    </ScrollViewer>

ViewBox:

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Viewbox Grid.Row="0" Grid.Column="0" Stretch="None">

            <Button Width="100" Height="50" Content="None"></Button>

        </Viewbox>

        <Viewbox Grid.Row="0" Grid.Column="1" Stretch="Uniform">

            <Button Width="100" Height="50" Content="Uniform"></Button>

        </Viewbox>

    </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">

    <Window.Resources>

        <Style TargetType="Button">/设置样式的类型,全局样式

            <Setter Property="Background" Value="WhiteSmoke"></Setter>//设置背景属性的样式

            <Setter Property="FontSize"  Value="20"></Setter>//设置文本字体的样式

            <Setter Property="Margin"  Value="10, 20"></Setter>//设置边框的外部样式

        </Style>

        <Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">//绑定单个样式

            <Setter Property="Background" Value="CadetBlue"></Setter>

        </Style>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Button Style="{StaticResource loginStyle}" Content="Button1" Grid.Column="0" Grid.Row="1" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="2" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="0" Width="Auto" Height="Auto" ></Button>

    </Grid>

</Window> 

外部样式:

首先创建一个xaml文件

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   

        <Style TargetType="Button">

            <Setter Property="Background" Value="WhiteSmoke"></Setter>

            <Setter Property="FontSize"  Value="20"></Setter>

            <Setter Property="Margin"  Value="10, 20"></Setter>

        </Style>

        <Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">

            <Setter Property="Background" Value="CadetBlue"></Setter>

        </Style>

</ResourceDictionary>

然后在App.xaml种添加

引用路径

<Application x:Class="WpfApp2.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:local="clr-namespace:WpfApp2"

             StartupUri="MainWindow.xaml">

    <Application.Resources>

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>

                <ResourceDictionary Source="/WpfApp2;component/Dictionary1.xaml"/>

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>

    </Application.Resources>

</Application>

<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>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Button Style="{StaticResource loginStyle}" Content="Button1" Grid.Column="0" Grid.Row="1" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="2" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="0" Width="Auto" Height="Auto" ></Button>

    </Grid>

</Window>

自定义样式模板及触发器

<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>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>

<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>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

             

                        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>

触发器绑定:

<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>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border x:Name="boder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

             

                        <TextBlock x:Name="txt" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="true">//绑定鼠标移动

                            <Setter TargetName="boder" Property="Background" Value="Blue"/>

                         <Setter TargetName="txt" Property="FontSize" Value="20"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="true">//绑定鼠标点下去的

                            <Setter TargetName="txt" Property="Background" Value="red"/>

                         <Setter TargetName="txt" Property="FontSize" Value="20"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>


http://www.ppmy.cn/news/1429030.html

相关文章

SPARK:性能调优之RSS

参考文章&#xff1a; 京东Spark自研Remote Shuffle Service在大促中的应用实践SPARK RSS 杂谈RSS设计文档 一、原生的 shuffle 过程 1、在mapper将shuffle数据写到本地磁盘&#xff0c;每个mapper会按照下游reducer的个数生成block 2、reducer在fetch环节拉取对应的block …

Linux--链表 第二十五天

1. 链表 t1.next -> data t1.next->next->data .(点号)的优先级比->的大 所以 t1.next->data 就可以了 不用(t1.next)->data 2. 链表的静态增加和动态遍历 打印链表算法&#xff0c; void printLink(struct Test *head) { struct Te…

AlDente Pro for mac最新激活版:电池长续航软件

AlDente Pro是一款专为Mac用户设计的电池管理工具&#xff0c;旨在提供电池安全和健康管理的一站式解决方案。它具备实时监控电池状态的功能&#xff0c;让用户随时了解电池的电量、充电次数、健康状态等信息。 AlDente Pro for mac最新激活版下载 同时&#xff0c;AlDente Pro…

BOM事件的重点——之转生在异世界学前端

每个事件都有事件源&#xff0c;事件类型&#xff0c;事件处理程序 事件源指触发事件的元素&#xff0c;事件类型值什么事件&#xff0c;事件处理程序指事件触发要执行的代码 每一个事件类型都有一个事件对象&#xff0c;事件对象是事件源触发产生的对象 事件对象其实指的是…

Centos7 搭建 GitLab服务 下载-安装-配置-卸载 完整版

说明 本文介绍一下 在CentOS7 上执行 GitLab 服务器的离线安装步骤。 本文介绍的步骤适用于 gitlab-ce-10.0.0 至 gitlab-ce-16.xx.xx 版本。 本文详细记录了安装的全部过程&#xff0c;各位读者可以直接1.环境准备 安装依赖 yum install -y curl policycoreutils-python ope…

5.Eureka原理分析

消费者如何获取服务提供者具体信息&#xff1f; 1.服务提供者启动时向Eureka注册自己的信息。 2.Eureka保存这些信息。 3.消费者根据服务名称向Eureka拉取提供者信息。 如果有多个服务的提供者&#xff0c;消费者该如何选择&#xff1f; 1.服务消费者利用负载均衡算法&…

Oracle Hint 语法详解

什么是Hint Hint 是 Oracle 提供的一种 SQL 语法&#xff0c;它允许用户在 SQL 语句中插入相关的语法&#xff0c;从而影响 SQL 的执行方式。 因为 Hint 的特殊作用&#xff0c;所以对于开发人员不应该在代码中使用它&#xff0c;Hint 更像是 Oracle 提供给 DBA 用来分析诊断问…

MongoDB与MySQL的区别???MongoDB的优势???

MongoDB是一种开源的文档型数据库管理系统&#xff0c;它使用类似于JSON的BSON格式&#xff08;Binary JSON&#xff09;来存储数据。与传统关系型数据库不同&#xff0c;MongoDB不使用表和行的结构&#xff0c;而是采用集合&#xff08;Collection&#xff09;(Mysql表)和文档…