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

网站流量分布/百度优化是什么意思

网站流量分布,百度优化是什么意思,淘宝网站边上的导航栏怎么做,河南郑州静默管理本文介绍在 MVVM 中,如何用 ViewModel 控制焦点。 这焦点设置个东西嘛,有些争论。就是到底要不要用 ViewModel 来控制视图的键盘输入焦点。这里不讨论,假设你就是要通过 VM,设置输入焦点在哪里。 MSDN 有解释关于 Focus 的&#x…

本文介绍在 MVVM 中,如何用 ViewModel 控制焦点。

这焦点设置个东西嘛,有些争论。就是到底要不要用 ViewModel 来控制视图的键盘输入焦点。这里不讨论,假设你就是要通过 VM,设置输入焦点在哪里。

MSDN 有解释关于 Focus 的,还有 FocusManager,点击这里打开。不知道的话建议你先看看,只求结果的可以直接看下面代码,抄就是了。这次,初级的解释全部略过,直接说做法,看不懂的请留言。做法很多,大概两种比较符合 MVVM 模式:

1. DataTrigger 设置 FocusedElement

Style 中写 Trigger,DataTrigger,值变化触发设置 FocusManager 的 FocusedElement。

XAML:

 

<Grid><Grid.Style><Style><Style.Triggers><DataTrigger Binding="{Binding Path=ScanFtBarCodeNow}" Value="True"><Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=FtNoTextBox}"/></DataTrigger></Style.Triggers></Style></Grid.Style><TextBox name="FtNoTextBox"/></Grid>

 

注意:放置 Style 的位置要注意。一层又一层 Grid 的话,FocusManager 单单设置 FocusedElement 为 TextBox 不足够。原因在 MSDN 有说明,不重复。

2. Attached Property

使用 Attached Property 为控件加属性,值变化触发 CallBack,然后 UIElement.Focus()。所谓值变化,比如,bool 变为 True 。

CODE:

publicstaticclass FocusBehavior {publicstaticbool GetIsFocused(DependencyObject obj) {return (bool)obj.GetValue(IsFocusedProperty);}publicstaticvoid SetIsFocused(DependencyObject obj, bool value) {obj.SetValue(IsFocusedProperty, value);}publicstaticreadonly DependencyProperty IsFocusedProperty =DependencyProperty.RegisterAttached("IsFocused", typeof(bool),typeof(FocusBehavior),new UIPropertyMetadata(false, (s, e) => {var sender = (UIElement)s;if ((bool)e.NewValue) {sender.Focus();Keyboard.Focus(sender);}}));
}

 

XAML:

<TextBox local:FocusBehavior.IsFocused="{Binding Path=ScanBarCodeNow}"/>

这做法虽然简单,但实际绑定的并非 UIElement.IsFocused 这只读属性,看CallBack 代码就知道,两个Focus() 都只是方法。界面操作焦点离开此控件时候,DependencyProperty 的值不会变化。后果是,设置一次 true 后,属性值 true,焦点离开它依然是 true,你再设置它 true,会被认定为值无变化,CallBack 没有执行,Focus() 不会运行。Workaround:先设 false 再设 true。

好傻吧。改一下,虽然 IsFocus 是只读无法直接绑定去改它,但可以把 GotFocus 和 LostFocus 事件接上此 Attached Property,触发变更 DependencyProperty 的值,使它变成双向绑定。

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;namespace Lepton_Practical_MVVM_5 {publicstaticclass FocusBehavior {privatestatic Dictionary<UIElement, RoutedEventHandler> handlers =new Dictionary<UIElement, RoutedEventHandler>();publicstaticbool? GetIsFocused(DependencyObject obj) {return (bool?)obj.GetValue(IsFocusedProperty);}publicstaticvoid SetIsFocused(DependencyObject obj, bool? value) {obj.SetValue(IsFocusedProperty, value);}publicstaticreadonly DependencyProperty IsFocusedProperty =DependencyProperty.RegisterAttached("IsFocused",typeof(bool?),typeof(FocusBehavior),new UIPropertyMetadata() {DefaultValue =null,PropertyChangedCallback =(s, e) => {UIElement sender = (UIElement)s;RoutedEventHandler x;if (!handlers.TryGetValue(sender, out x)) {Attach(sender);}if ((bool)e.NewValue) {sender.Focus();Keyboard.Focus(sender);}}});privatestaticvoid Attach(UIElement sender) {RoutedEventHandler handler = (s, e) => {UIElement ui = (UIElement)s;if (e.RoutedEvent == UIElement.GotFocusEvent) {ui.SetValue(IsFocusedProperty, true);}if (e.RoutedEvent == UIElement.LostFocusEvent) {ui.SetValue(IsFocusedProperty, false);}};sender.GotFocus += handler;sender.LostFocus += handler;handlers.Add(sender, handler);}}
}

Dictionary 为了记下已连接的 Handler ,以免重复加入。

XAML:

<Window x:Class="Lepton_Practical_MVVM_5.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:Lepton_Practical_MVVM_5"Title="MainWindow" Height="350" Width="525"><Grid><TextBox local:FocusBehavior.IsFocused="{Binding Path=IsTextBox1Focus,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="49,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"/></Grid></Window>

点击这里下载Attached Property 版本的代码。

这做法是只有加句柄没有移除,你有情况需要移除的话,自己改吧。

我在这群里,欢迎加入交流:
开发板玩家群 578649319开发板玩家群 578649319
硬件创客 (10105555)硬件创客 (10105555)

转载于:https://www.cnblogs.com/leptonation/p/3292961.html

http://www.jmfq.cn/news/5010913.html

相关文章:

  • 黄石网站建设报价/高权重网站出售
  • 江西网站建设费用/推广项目网站
  • 武汉网站建设方法/百度本地推广
  • 可以建网站的路由器/nba最新消息交易
  • 郑州做网站费用/徐州seo排名公司
  • 品牌内容包括哪些/江门seo网站推广
  • 局域网网页制作工具/页面优化的方法有哪些
  • 合肥设计网站/今日头条郑州头条新闻
  • 做设计在哪个网站找图片大全/国内十大软件培训机构
  • asp.net 怎么做网站/公关公司经营范围
  • 小红书seo是什么/福建网络seo关键词优化教程
  • 电话销售做网站打官司/外贸seo优化公司
  • 南昌市网站建设公司/windows优化大师可以卸载吗
  • 从学习php到可以独立做网站需要多久/近期的时事热点或新闻事件
  • 益阳网站建设哪里好/网站注册页面
  • 织梦做中英文网站/b2b网站源码
  • 如何建设一个双语的网站/数字营销案例
  • 南宁网站建设方案报价/郑州做网站推广哪家好
  • 企业网站内容/网络营销理论
  • 天津公司做网站/湖南关键词优化首选
  • 有内涵的公司名字/西安网站seo推广
  • 平面设计可以做网站?/品牌咨询
  • 空投注册送币网站怎么做/百度搜索榜
  • 天津做网站找哪家公司/百度资源共享链接分享组
  • wordpress站点网站地图/网络舆情分析报告范文
  • 做ppt素材的网站有哪些/关键词优化网站排名
  • 代理公司注册后果严重/seo外链平台
  • 网站开发说明书模板/seo优
  • 济南cms建站/佛山网站优化
  • 微信网站应用开发/seo培训课程