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

桥头东莞网站建设弄一个微信小程序多少钱

桥头东莞网站建设,弄一个微信小程序多少钱,微商引流人脉推广软件,公司网站开发策划书感谢酪酪烤奶 提供的Solution 文章目录 感谢酪酪烤奶 提供的Solution使用示例示例代码分析各类交互流程 WPF DataGrid 列宽绑定机制分析整体架构数据流分析1. ViewModel到Slider的绑定2. ViewModel到DataGrid列的绑定a. 绑定代理(BindingProxy)b. 列宽绑定c. 数据流 关键机制详…感谢酪酪烤奶 提供的Solution 文章目录 感谢酪酪烤奶 提供的Solution使用示例示例代码分析各类交互流程 WPF DataGrid 列宽绑定机制分析整体架构数据流分析1. ViewModel到Slider的绑定2. ViewModel到DataGrid列的绑定a. 绑定代理(BindingProxy)b. 列宽绑定c. 数据流 关键机制详解1. BindingProxy的作用2. DataGridHelper附加属性3. 数据关联路径为什么这样设计 解决方案分析核心问题分析关键解决方案组件1. **BindingProxy类Freezable辅助类**2. **DoubleToDataGridLengthConverter转换器**3. **DataGridHelper附加属性**4. **XAML中的关键绑定修改** 为什么这个方案有效 使用示例 Window x:ClassWpfApp1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp1TitleDataGrid列宽绑定示例 Height450 Width800Window.Resources!-- 创建绑定代理 --local:BindingProxy x:KeyProxy Data{Binding}/!-- 列宽转换器 --local:DoubleToDataGridLengthConverter x:KeyDoubleToDataGridLengthConverter//Window.ResourcesGridGrid.RowDefinitionsRowDefinition HeightAuto/RowDefinition Height*//Grid.RowDefinitions!-- 列宽调整滑块 --StackPanel OrientationHorizontal Margin10TextBlock Text姓名列宽度: VerticalAlignmentCenter Margin0,0,10,0/Slider Minimum50 Maximum300 Value{Binding NameColumnWidth, ModeTwoWay} Width200 Margin0,10/TextBlock Text{Binding NameColumnWidth, StringFormat{}{0}px} VerticalAlignmentCenter Margin10,0,0,0//StackPanel!-- DataGrid控件 --DataGrid ItemsSource{Binding People} AutoGenerateColumnsFalse Grid.Row1 Margin10DataGrid.Columns!-- 使用TemplateColumn并通过代理绑定Width属性 --DataGridTemplateColumn Header姓名 local:DataGridHelper.BindableWidth{Binding Data.NameColumnWidth, Source{StaticResource Proxy},Converter{StaticResource DoubleToDataGridLengthConverter}}DataGridTemplateColumn.CellTemplateDataTemplateTextBlock Text{Binding Name} Margin5//DataTemplate/DataGridTemplateColumn.CellTemplate/DataGridTemplateColumnDataGridTextColumn Header年龄 Binding{Binding Age} Width100/DataGridTextColumn Header职业 Binding{Binding Occupation} Width150//DataGrid.Columns/DataGrid/Grid /Window using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Data;namespace WpfApp1 {public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext new MainViewModel();}}public class MainViewModel : INotifyPropertyChanged{private double _nameColumnWidth 150;public double NameColumnWidth{get { return _nameColumnWidth; }set{if (_nameColumnWidth ! value){_nameColumnWidth value;OnPropertyChanged(nameof(NameColumnWidth));}}}public ObservableCollectionPerson People { get; set; }public MainViewModel(){People new ObservableCollectionPerson{new Person { Name 张三, Age 25, Occupation 工程师 },new Person { Name 李四, Age 30, Occupation 设计师 },new Person { Name 王五, Age 28, Occupation 产品经理 }};}public event PropertyChangedEventHandler? PropertyChanged;protected virtual void OnPropertyChanged(string propertyName){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}public class Person{public string Name { get; set; }public int Age { get; set; }public string Occupation { get; set; }}// 列宽转换器public class DoubleToDataGridLengthConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value is double doubleValue){return new DataGridLength(doubleValue);}return DependencyProperty.UnsetValue;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){if (value is DataGridLength dataGridLength){return dataGridLength.Value;}return DependencyProperty.UnsetValue;}}// 绑定代理类public class BindingProxy : Freezable{protected override Freezable CreateInstanceCore(){return new BindingProxy();}public object Data{get { return (object)GetValue(DataProperty); }set { SetValue(DataProperty, value); }}public static readonly DependencyProperty DataProperty DependencyProperty.Register(Data, typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));}// 关键修改添加附加属性来处理列宽绑定public static class DataGridHelper{public static readonly DependencyProperty BindableWidthProperty DependencyProperty.RegisterAttached(BindableWidth,typeof(DataGridLength),typeof(DataGridHelper),new PropertyMetadata(new DataGridLength(1, DataGridLengthUnitType.SizeToHeader), OnBindableWidthChanged));public static DataGridLength GetBindableWidth(DependencyObject obj){return (DataGridLength)obj.GetValue(BindableWidthProperty);}public static void SetBindableWidth(DependencyObject obj, DataGridLength value){obj.SetValue(BindableWidthProperty, value);}private static void OnBindableWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is DataGridColumn column){column.Width (DataGridLength)e.NewValue;}}} } 示例代码分析 各类交互流程 WPF DataGrid 列宽绑定机制分析 这段代码实现了通过ViewModel属性动态控制DataGrid列宽的功能下面我将详细分析Width是如何被更新的以及Data是如何关联起来的。 整体架构 代码主要包含以下几个关键部分 MainWindow.xaml定义UI结构和绑定MainViewModel提供数据和NameColumnWidth属性BindingProxy解决DataContext绑定问题DataGridHelper实现列宽绑定的附加属性DoubleToDataGridLengthConverter类型转换器 数据流分析 1. ViewModel到Slider的绑定 Slider Value{Binding NameColumnWidth, ModeTwoWay} /Slider的Value属性双向绑定到ViewModel的NameColumnWidth属性当用户拖动滑块时NameColumnWidth会被更新同时TextBlock显示当前宽度值也是绑定到同一属性 2. ViewModel到DataGrid列的绑定 这是最复杂的部分涉及多层绑定 a. 绑定代理(BindingProxy) local:BindingProxy x:KeyProxy Data{Binding}/创建了一个BindingProxy实例其Data属性绑定到当前DataContext这使得在DataGrid列定义中可以通过静态资源访问ViewModel b. 列宽绑定 local:DataGridHelper.BindableWidth{Binding Data.NameColumnWidth, Source{StaticResource Proxy}}使用DataGridHelper.BindableWidth附加属性绑定路径为Data.NameColumnWidth通过Proxy访问这意味着实际上绑定到ViewModel的NameColumnWidth属性 c. 数据流 用户拖动Slider → NameColumnWidth更新由于Proxy.Data绑定到整个DataContextProxy能感知到变化BindableWidth属性通过Proxy获取到新的NameColumnWidth值DataGridHelper的OnBindableWidthChanged回调被触发回调中将新的值赋给DataGridColumn.Width 关键机制详解 1. BindingProxy的作用 BindingProxy解决了DataGrid列定义中无法直接访问DataContext的问题 DataGrid列不是可视化树的一部分没有继承DataContext通过创建Proxy作为静态资源绑定到当前DataContext然后在列绑定中通过Source{StaticResource Proxy}访问 2. DataGridHelper附加属性 这是实现列宽绑定的核心 定义BindableWidth附加属性当属性值变化时OnBindableWidthChanged回调被触发回调中将新值赋给DataGridColumn的Width属性 private static void OnBindableWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {if (d is DataGridColumn column){column.Width (DataGridLength)e.NewValue;} }3. 数据关联路径 完整的绑定路径是 Slider.Value → ViewModel.NameColumnWidth → Proxy.Data.NameColumnWidth → DataGridHelper.BindableWidth → DataGridColumn.Width 为什么这样设计 解决DataContext问题DataGrid列不在可视化树中无法直接绑定到ViewModel类型兼容DataGridColumn.Width是DataGridLength类型而Slider操作的是double重用性通过附加属性和代理可以方便地在其他地方重用这种绑定方式 解决方案分析 问题涉及WPF中两个复杂的技术点DataGridTemplateColumn的特殊绑定行为和属性变更通知机制。 核心问题分析 最初遇到的问题是由以下因素共同导致的 DataGridTemplateColumn不在可视化树中 这导致它无法通过RelativeSource或ElementName绑定到窗口或DataGrid的DataContext。 Width属性类型不匹配 DataGridColumn.Width属性类型是DataGridLength直接绑定了double类型需要类型转换。 列宽属性变更通知缺失 即使绑定成功DataGridTemplateColumn的Width属性默认不会自动响应绑定源的变化。 关键解决方案组件 1. BindingProxy类Freezable辅助类 public class BindingProxy : Freezable {protected override Freezable CreateInstanceCore(){return new BindingProxy();}public object Data{get { return (object)GetValue(DataProperty); }set { SetValue(DataProperty, value); }}public static readonly DependencyProperty DataProperty DependencyProperty.Register(Data, typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); }作用 通过继承Freezable这个类能够存在于资源树中而非可视化树从而突破DataGridTemplateColumn的绑定限制。它捕获窗口的DataContext并使其可被模板列访问。 2. DoubleToDataGridLengthConverter转换器 public class DoubleToDataGridLengthConverter : IValueConverter {public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value is double doubleValue){return new DataGridLength(doubleValue);}return DependencyProperty.UnsetValue;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){if (value is DataGridLength dataGridLength){return dataGridLength.Value;}return DependencyProperty.UnsetValue;} }作用 将ViewModel中的double类型属性转换为DataGridLength类型解决类型不匹配问题。 3. DataGridHelper附加属性 public static class DataGridHelper {public static readonly DependencyProperty BindableWidthProperty DependencyProperty.RegisterAttached(BindableWidth,typeof(DataGridLength),typeof(DataGridHelper),new PropertyMetadata(new DataGridLength(1, DataGridLengthUnitType.SizeToHeader), OnBindableWidthChanged));public static DataGridLength GetBindableWidth(DependencyObject obj){return (DataGridLength)obj.GetValue(BindableWidthProperty);}public static void SetBindableWidth(DependencyObject obj, DataGridLength value){obj.SetValue(BindableWidthProperty, value);}private static void OnBindableWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is DataGridColumn column){column.Width (DataGridLength)e.NewValue;}} }作用 通过附加属性机制创建一个可绑定的BindableWidth属性并在属性值变化时强制更新列宽。这解决了列宽不响应绑定变化的问题。 4. XAML中的关键绑定修改 Window.Resourceslocal:DoubleToDataGridLengthConverter x:KeyDoubleToDataGridLengthConverter/local:BindingProxy x:KeyProxy Data{Binding}/ /Window.ResourcesDataGridTemplateColumn Header姓名 local:DataGridHelper.BindableWidth{Binding Data.NameColumnWidth, Source{StaticResource Proxy}, Converter{StaticResource DoubleToDataGridLengthConverter}}绑定路径解析 Source{StaticResource Proxy}从资源中获取BindingProxy实例Data.NameColumnWidth通过Proxy的Data属性访问ViewModel的NameColumnWidth属性Converter将double转换为DataGridLengthlocal:DataGridHelper.BindableWidth使用附加属性而非直接设置Width 为什么这个方案有效 突破可视化树限制 通过BindingProxy我们将DataContext从资源树引入避开了DataGridTemplateColumn不在可视化树中的问题。 类型安全转换 转换器确保了从double到DataGridLength的正确类型转换。 强制属性更新 附加属性的PropertyChangedCallbackOnBindableWidthChanged在值变化时主动更新列宽解决了通知缺失问题。
http://www.hkea.cn/news/14418934/

相关文章:

  • 北理工网站开发与运用陌陌网站开发成本
  • 个人如何在企业网站做实名认证数码网站建设
  • 别人把我做的网站_我自己现在想把网站背景改掉_我要怎么改app开发一般需要多少钱
  • 做网站长尾词易安卓开发app稳定吗
  • 如何建设景区旅游网站个人网站作品下载
  • 做游戏网站赚钱么食品招商网
  • 做后台系统的网站网站制作制作
  • 积分网站运营建设投标书网站开发的硬件环境是什么
  • 适合做公司网站的cms肥乡网站建设
  • 优化网站广告优化代理网站备案收钱
  • 阿里巴巴做网站接单几率wordpress模板工作室
  • 长兴网站建设列举企业网站建设有哪些好处网上有哪些购物平台
  • 江苏城乡建设教育网站用手机设计装修的软件
  • 云南公司网站制作网站跳出率很高
  • 电商网站设计模板dwwordpress母狗
  • 网站优化要多少钱学做网站教学百度网盘
  • dw怎样做网站链接网站信息化建设具体内容
  • wordpress 建网站签订网站建设合同
  • 天津市建设工程交易中心网站服务商英文
  • 沈阳正规制作网站公司哪家好深圳网站设计公司费用
  • 区域教育联盟网站建设中国现货交易网官网
  • 有哪些专门做展会创意的网站企业官网首页模板
  • 小网站发布要怎么做杭州 手机网站
  • 浏阳市住房和城乡建设局的网站广告公司做网站的效果怎么样
  • 珠海移动网站建设公司排名wordpress 选择用户登录
  • 自己可以学做网站吗品牌外贸网站建设
  • 泰州市建设监理协会网站wordpress 导航插件
  • 网站建设哪里实惠商务网站开发背景
  • 建网站排名建设工程j教育网站
  • 室内设计网站界面oppo开放平台