当前位置: 首页 > 编程日记 > 正文

WPF入门教程系列九——布局之DockPanel与ViewBox(四)

七. DockPanel

DockPanel定义一个区域,在此区域中,您可以使子元素通过描点的形式排列,这些对象位于 Children 属性中。停靠面板其实就是在WinForm类似于Dock属性的元 素。DockPanel会对每个子元素进行排序,并停靠在面板的一侧,多个停靠在同侧的元素则按顺序排序。

如果将 LastChildFill 属性设置为 true(默认设置),那么无论对 DockPanel 的最后一个子元素设置的其他任何停靠值如何,该子元素都将始终填满剩余的空间。若要将子元素停靠在另一个方向,必须将 LastChildFill 属性设置为 false,还必须为最后一个子元素指定显式停靠方向。

默认情况下,面板元素并不接收焦点。要强制使面板元素接收焦点,请将 Focusable 属性设置为 true。

注意:屏幕上 DockPanel 的子元素的位置由相关子元素的 Dock 属性以及这些子元素在 DockPanel 下的相对顺序确定。因此,具有相同 Dock 属性值的一组子元素在屏幕上的位置可能不同,具体取决于这些子元素在 DockPanel 下的顺序。子元素的顺序会影响定位,因为 DockPanel 会按顺序迭代其子元素,并根据剩余空间来设置每个子元素的位置。

使用XAML代码实现如下图效果。图如下。

复制代码
<Window x:Class="WpfApp1.WindowDock"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowDock" Height="300" Width="400"><Grid><DockPanel Width="Auto" Height="Auto"><Button DockPanel.Dock="Left" Content="1" /><Button DockPanel.Dock="Top" Content="2" /><Button DockPanel.Dock="Right" Content="3" /><Button DockPanel.Dock="Bottom" Content="4" /><Button  HorizontalAlignment="Left"  Name="btnAddByCode" Height="22" Width="65" DockPanel.Dock=" Left "  Click="btnAddByCode_Click" >后台代码添加</Button></DockPanel></Grid></Window>
复制代码

使用C#代码实现如下图效果。图如下。

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace WpfApp1{/// <summary>/// WindowDock.xaml 的交互逻辑/// </summary>public partial class WindowDock : Window{public WindowDock(){InitializeComponent();}private void btnAddByCode_Click(object sender, RoutedEventArgs e){DockPanel dp = new DockPanel();//  dp.LastChildFill = true;dp.Width = Double.NaN;    //相当于在XAML中设置Width="Auto"dp.Height = Double.NaN;   //相当于在XAML中设置Height="Auto"//把dp添加为窗体的子控件this.Content = dp;//添加RectanglesRectangle rTop = new Rectangle();rTop.Fill = new SolidColorBrush(Colors.BlanchedAlmond);rTop.Stroke = new SolidColorBrush(Colors.BlanchedAlmond);rTop.Height = 30;dp.Children.Add(rTop);rTop.SetValue(DockPanel.DockProperty, Dock.Top);Rectangle rLeft = new Rectangle();rLeft.Fill = new SolidColorBrush(Colors.Gray);rLeft.Stroke = new SolidColorBrush(Colors.Gray);rLeft.HorizontalAlignment = HorizontalAlignment.Left;rLeft.Height = 30;rLeft.Width = 30;dp.Children.Add(rLeft);rLeft.SetValue(DockPanel.DockProperty, Dock.Left);Rectangle rBottom = new Rectangle();rBottom.Fill = new SolidColorBrush(Colors.Red);rBottom.VerticalAlignment = VerticalAlignment.Bottom;rBottom.Height = 30;dp.Children.Add(rBottom);rBottom.SetValue(DockPanel.DockProperty, Dock.Bottom);}}}
复制代码

八. ViewBox

ViewBox这个控件通常和其他控件结合起来使用,是WPF中非常有用的控件。定义一个内容容器。ViewBox组件的作用是拉伸或延展位于其中的组件,以填满可用空间,使之有更好的布局及视觉效果。

一个 Viewbox中只能放一个控件。如果多添加了一个控件就会报错。如下图。

组件常用属性:

Child:获取或设置一个ViewBox元素的单一子元素。

Stretch:获取或设置拉伸模式以决定该组件中的内容以怎样的形式填充该组件的已有空间。具体设置值如下:

成员名称

说明

None

内容保持其原始大小。

Fill

调整内容的大小以填充目标尺寸。 不保留纵横比。

Uniform

在保留内容原有纵横比的同时调整内容的大小,以适合目标尺寸。

UniformToFill

在保留内容原有纵横比的同时调整内容的大小,以填充目标尺寸。 如果目标矩形的纵横比不同于源矩形的纵横比,则对源内容进行剪裁以适合目标尺寸。

StretchDirection:获取或设置该组件的拉伸方向以决定该组件中的内容将以何种形式被延展。具体的设置值如下。

成员名称

说明

UpOnly

仅当内容小于父项时,它才会放大。 如果内容大于父项,不会执行任何缩小操作。

DownOnly

仅当内容大于父项时,它才会缩小。 如果内容小于父项,不会执行任何放大操作。

Both

内容根据 Stretch 属性进行拉伸以适合父项的大小。

接下来我们做个示例,你可以通过选择下拉框中的不同设置值,来查看不同的效果。效果如下图。

XAML代码实现:

复制代码
<Window x:Class="WpfApp1.WindowViewBox"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowViewBox" Height="400" Width="500" Loaded="Window_Loaded"><Grid><Grid.RowDefinitions><RowDefinition Height="250"/><RowDefinition Height="auto"/><RowDefinition Height="73*"/></Grid.RowDefinitions><Viewbox Stretch="Fill" Grid.Row="0" Name="viewBoxTest"><TextBox Text="通过调查发现,被阿里打假驱逐的30家售假商家中,竟有12家转战到了京东上。" /></Viewbox><WrapPanel  Grid.Row="2"><StackPanel><TextBlock Height="16" HorizontalAlignment="Left"  VerticalAlignment="Bottom" Width="66" Text="拉伸模式:" TextWrapping="Wrap"/><ComboBox x:Name="cbStretch" Height="21" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="139" SelectionChanged="cbStretch_SelectionChanged"/></StackPanel><StackPanel><TextBlock Height="16" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Width="56" Text="拉伸方向:" TextWrapping="Wrap"/><ComboBox x:Name="cbStretchDirection" Height="21" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Width="139" SelectionChanged="cbStretchDirection_SelectionChanged"/></StackPanel></WrapPanel></Grid></Window>
复制代码

c#代码实现:

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace WpfApp1{/// <summary>/// WindowViewBox.xaml 的交互逻辑/// </summary>public partial class WindowViewBox : Window{//定义cbStretch与cbStretchDirection的数据源 List<StretchHelper> cbStretchList = new List<StretchHelper>(); List<StretchDirectionHelper> cbStretchDirectionList = new List<StretchDirectionHelper>(); public WindowViewBox(){InitializeComponent();}private void BindDrp(){ //填充各ComboBox内容 cbStretchList.Add(new StretchHelper() { StretchModeName = "Fill", theStretchMode = Stretch.Fill });cbStretchList.Add(new StretchHelper() { StretchModeName = "None", theStretchMode = Stretch.None });cbStretchList.Add(new StretchHelper() { StretchModeName = "Uniform", theStretchMode = Stretch.Uniform });cbStretchList.Add(new StretchHelper() { StretchModeName = "UniformToFill", theStretchMode = Stretch.UniformToFill });cbStretch.ItemsSource = cbStretchList;cbStretch.DisplayMemberPath = "StretchModeName";cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "DownOnly", theStretchDirection = StretchDirection.DownOnly });cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "UpOnly", theStretchDirection = StretchDirection.UpOnly });cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "Both", theStretchDirection = StretchDirection.Both });cbStretchDirection.ItemsSource = cbStretchDirectionList;cbStretchDirection.DisplayMemberPath = "StretchDirectionName";}private void cbStretchDirection_SelectionChanged(object sender, SelectionChangedEventArgs e){if (cbStretchDirection.SelectedItem != null){viewBoxTest.StretchDirection = (cbStretchDirection.SelectedItem as StretchDirectionHelper).theStretchDirection;} }private void cbStretch_SelectionChanged(object sender, SelectionChangedEventArgs e){if (cbStretch.SelectedItem != null){viewBoxTest.Stretch = (cbStretch.SelectedItem as StretchHelper).theStretchMode;} }private void Window_Loaded(object sender, RoutedEventArgs e){BindDrp();}}//辅助类StretchHelper public class StretchHelper{public string StretchModeName { get; set; }public Stretch theStretchMode { get; set; }}//辅助类StretchDirectionHelper public class StretchDirectionHelper{public string StretchDirectionName { get; set; }public StretchDirection theStretchDirection { get; set; }} }
复制代码

转载于:https://www.cnblogs.com/zzw1986/p/7583516.html

相关文章:

tabBar 自定义,小程序自定义底部导航栏

创建一个自定义组件 my_tab&#xff0c;组件代码在后面&#xff0c;先看调用自定义组件的代码&#xff0c;比如我需要在index 页面调用&#xff0c;就在index.json中引用组件&#xff0c;index.json 代码&#xff08;引用的路径为你创建的自定义组件路径&#xff09; {"n…

2015年最新出炉的JavaScript开发框架

前端框架简化了开发过程中&#xff0c;像 Bootstrap 和 Foundation 就是前端框架的佼佼者。在这篇文章了&#xff0c;我们编制了一组新鲜的&#xff0c;实用的&#xff0c;可以帮助您建立高质量的 Web 应用程序的 JavaScript 框架清单。 1.Aurelia Aurelia是下一代JavaScript客…

小程序前端性能测试_如何提高前端应用程序的性能

小程序前端性能测试If your website takes longer than 3 seconds to load, you could already be losing nearly half of your visitors.如果您的网站加载时间超过3秒&#xff0c;则可能已经失去了将近一半的访问者。 Yes this is a fact, proven by several research studie…

10-TypeScript中的接口

接口是一种规约的约定&#xff0c;从接口继承的类必须实现接口的约定。在高级开发中&#xff0c;通常接口是用于实现各种设计模式的基础&#xff0c;没有接口&#xff0c;设计模式无从谈起。 定义接口&#xff1a; interface ILog{recordlog():boolean; } 类从接口继承&#xf…

样式集(六)仿微信通讯录样式

效果图&#xff1a; 这里有引用到 自定义底部导航&#xff0c;自定义底部导航组件链接 <!--pages/chatList/chatList.wxml--><!-- <include src"/components/common/common" /> --> <view class"top"><view class"pageTi…

WCF动态添加ServiceKnownType

WCF中传输自定义类型时&#xff0c;必须在服务接口类&#xff08;服务协定&#xff09;上加上ServiceKnownType(typeof(yourClass)), 在实际应用中比较麻烦&#xff0c;可以用动态的办法来实现动态添加。 服务接口类&#xff0c;加上一行 [ServiceKnownType("GetKnownType…

博客 rss 如何使用_如何使用RSS从您的GatsbyJS博客自动交叉发布

博客 rss 如何使用With the recent exodus from Medium many developers are now creating their own GatsbyJS Blogs and then cross-posting to Medium or publications like freecodecamp.org and dev.to.随着Medium最近的离职&#xff0c;许多开发人员现在正在创建自己的Ga…

大型技术网站的技术( 高并发、大数据、高可用、分布式....)(一)

面对高并发、大流量、高可用、海量数据、用户分布广泛、网络情况复杂这类网站系统我们如何应对&#xff1f;&#xff1f;&#xff1f; 第一阶段 一台服务器不行就上多台服务器 1.应用程序与数据服务分离 将应用程序、数据库、文件等资源放在一台服务器上&#xff0c;面对海量…

BestCoder Round #65 B C D || HDU 5591 5592 5593

B 题意&#xff1a;ZYB在远足中,和同学们玩了一个“数字炸弹”游戏&#xff1a;由主持人心里想一个在[1,N][1,N]中的数字XX&#xff0c;然后玩家们轮流猜一个数字&#xff0c;如果一个玩家恰好猜中XX则算负&#xff0c;否则主持人将告诉全场的人当前的数和XX比是偏大还是偏小&a…

数组去重,ES6数组去重 new Set()

普通数组去重 var b [...new Set([1,2, 3, 4, 5, 5, 5, 5])]console.log(b); 输出结果&#xff1a; 包含对象的数组去重 var o {a:1}var b [...new Set([o, o, 3, 4, 5, 5, 5, 5])]console.log(b); 输出结果&#xff1a; 包含对象的数组去重有一个坑 var b [...new Set([{…

使用angular的好处_在项目中使用Angular的最大好处

使用angular的好处by Irina Sidorenko伊琳娜西多连科(Irina Sidorenko) 在项目中使用Angular的最大好处 (The top benefits of using Angular for your project) 在项目实施中使用Angular的11个理由及其好处 (11 reasons to use Angular and its benefits for your project im…

python之路——模块和包

一、模块 1、什么是模块&#xff1f; 常见的场景&#xff1a;一个模块就是一个包含了Python定义和声明的文件&#xff0c;文件名就是模块名字加上.py的后缀。 但其实import加载的模块分为四个通用类别&#xff1a; 1、使用Python编写的代码&#xff08;.py文件&#xff09; 2、…

夺命雷公狗---linux NO:3 centos_mini版的安装和备份

废话不多说&#xff0c;和前面的其实是差不多的&#xff0c;如下图所示&#xff1a; 安装其实是和桌面版的差不多的&#xff0c;但是经典版的不能自定义分区&#xff08;如详细区&#xff0c;如home之类的&#xff09;。。。 因为我们使用的是命令行方式的所以直接选英文&#…

快速学习 async await 的使用, Demo 解析

async 和 await 字面都很好理解&#xff0c;分别是异步和等待。 来两个简单的 demo&#xff0c; demo1 tt2(){return new Promise(rps>{setTimeout(() > {rps(true)}, 1500);})},async tt1(){var a await this.tt2();console.log(a)},/*** 生命周期函数--监听页面加载*…

小型工作室创业项目_为什么新开发人员应该在小型创业公司工作

小型工作室创业项目In my first year of working in the industry (6 months as an intern, 6 months as a full-time employee), I worked at startups that were less than 10 people large. I was one of the only 2 or 3 developers, and usually one of the first. Throug…

head first python菜鸟学习笔记(第六章)

1. Python提供字典&#xff0c;允许有效组织数据&#xff0c;将数据与名关联&#xff0c;从而实现快速查找&#xff0c;而不是以数字关联。 字典是内置数据结构&#xff0c;允许将数据与键而不是数字关联。这样可以使内存中的数据与实际数据的结构保持一致。&#xff1f;&#…

小程序聊天室开发,发送文字,表情,图片,音频,视频,即时通讯,快速部署,可定制开发

效果图&#xff1a; 微信小程序聊天功能模块&#xff0c;现在已经支持发送图片&#xff0c;文字&#xff0c;音频&#xff0c;视频&#xff0c;表情&#xff0c;在线即时聊天啦。 需要做的可以联系我微信。13977284413 上代码&#xff1a; <view class"bo">…

常用浏览器插件

modify headers &#xff1a;firefox的IP伪造插件 httpRequester&#xff1a;firefox的模拟http请求插件JSON-handle&#xff1a;chrome格式化json插件firebug&#xff1a;firefox查看http请求工具firepath&#xff1a;firefox中获取元素路径转载于:https://www.cnblogs.com/xx…

编码中统一更该变量的快捷键_更多项目想法,以提高您的编码技能

编码中统一更该变量的快捷键Two weeks ago I published an article containing 15 project ideas that you can build to level up your coding skills, and people were very excited about that resource.两周前&#xff0c;我发表了一篇文章&#xff0c;其中包含15个项目构想…

My97DatePicker日历控件日报、每周和每月的选择

My97DatePicker日历控件日报、每周和每月的选择 1、设计源代码 <% page language"java" import"java.util.*" pageEncoding"UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><h…

DotNet Core Console 程序使用NLog

参考&#xff1a;https://github.com/NLog/NLog/wiki/Tutorial 步骤&#xff1a; 1. 使用Nuget安装NLog.Extensions.Logging Install-Package NLog.Extensions.Logging 2.编写代码&#xff08;到这步运行代码&#xff0c;不报错&#xff0c;但是也不会有log输出&#xff0c;因为…

小程序判断用户在线状态

在页面的两个生命周期组件里面 onShow() {console.log(-----上线线)let info wx.getStorageSync(chat_item)DB.collection(friends).where({_id: info._id}).get().then(res > {console.log(-----, res)if (res.data[0].a wx.getStorageSync(userInfo)._openid) {console.…

react.js做小程序_如何使用React.js构建现代的聊天应用程序

react.js做小程序In this tutorial, I will guide you to build your own group chat application using React, React Router, and CometChat Pro. Yes, rather than roll out our own server, we will instead use CometChat Pro to handle the real-time sending and receiv…

RAP Mock.js语法规范

Mock.js 的语法规范包括两部分&#xff1a; 数据模板定义规范&#xff08;Data Template Definition&#xff0c;DTD&#xff09;数据占位符定义规范&#xff08;Data Placeholder Definition&#xff0c;DPD&#xff09;1.数据模板定义规范 DTD 数据模板中的每个属性由 3 部分…

NSDictionary、NSMutableDictionary基本使用

郝萌主倾心贡献&#xff0c;尊重作者的劳动成果。请勿转载。假设文章对您有所帮助&#xff0c;欢迎给作者捐赠&#xff0c;支持郝萌主&#xff0c;捐赠数额任意&#xff0c;重在心意^_^ 我要捐赠: 点击捐赠Cocos2d-X源代码下载&#xff1a;点我传送游戏官方下载&#xff1a;htt…

h5轮播图及效果图

效果图&#xff1a; 代码&#xff1a; <!doctype html> <html><head><meta charset"utf-8"><title>jQuery响应式卡片轮播切换代码</title><link rel"stylesheet" type"text/css" href"css/style.c…

性能测试回归测试_自动网站性能回归测试

性能测试回归测试by Adam Henson亚当汉森(Adam Henson) 如何使用Foo自动执行网站性能回归测试 (How to automate website performance regression testing with Foo) 使用部署后步骤自动执行连续交付工作流程中的性能回归测试 (Using a post deploy step to automate performa…

【html】【13】特效篇--下拉导航

html代码&#xff1a; 1 <!DOCTYPE html>2 <html>3 <head>4 <meta http-equiv"Content-Type" content"text/html; charsetUTF-8">5 <title>Bootstrap导航条鼠标悬停下拉菜单</title>6 <li…

小程序获取用户所在城市完整代码

小程序目录结构 插入提示: 1. 申请开发者密钥&#xff08;key&#xff09;&#xff1a; 申请密钥 2. 下载微信小程序JavaScriptSDK&#xff0c;下载地址 下载完成后放入utils文件夹下引用即可 3. 安全域名设置&#xff0c;在“设置” -> “开发设置”中设置req…

prolog_如何通过观看权力的游戏学习Prolog

prologby Rachel Wiles瑞秋威尔斯(Rachel Wiles) 如何通过观看权力的游戏学习Prolog (How to learn Prolog by watching Game of Thrones) 他们死了吗&#xff1f; 他们还活着吗&#xff1f; 她是他的姨妈吗&#xff1f; 不用把精力浪费在2011年&#xff0c;而可以使用Prolog节…