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

网站后台密码忘了怎么办网络开发语言的有哪些

网站后台密码忘了怎么办,网络开发语言的有哪些,一个网站怎么做多条线路,wordpress您的主题支持一个菜单感谢酪酪烤奶 提供的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:Class="WpfApp1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"Title="DataGrid列宽绑定示例" Height="450" Width="800"><Window.Resources><!-- 创建绑定代理 --><local:BindingProxy x:Key="Proxy" Data="{Binding}"/><!-- 列宽转换器 --><local:DoubleToDataGridLengthConverter x:Key="DoubleToDataGridLengthConverter"/></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><!-- 列宽调整滑块 --><StackPanel Orientation="Horizontal" Margin="10"><TextBlock Text="姓名列宽度:" VerticalAlignment="Center" Margin="0,0,10,0"/><Slider Minimum="50" Maximum="300" Value="{Binding NameColumnWidth, Mode=TwoWay}" Width="200" Margin="0,10"/><TextBlock Text="{Binding NameColumnWidth, StringFormat={}{0}px}" VerticalAlignment="Center" Margin="10,0,0,0"/></StackPanel><!-- DataGrid控件 --><DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False" Grid.Row="1" Margin="10"><DataGrid.Columns><!-- 使用TemplateColumn并通过代理绑定Width属性 --><DataGridTemplateColumn Header="姓名" local:DataGridHelper.BindableWidth="{Binding Data.NameColumnWidth, Source={StaticResource Proxy},Converter={StaticResource DoubleToDataGridLengthConverter}}"><DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding Name}" Margin="5"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn Header="年龄" Binding="{Binding Age}" Width="100"/><DataGridTextColumn Header="职业" Binding="{Binding Occupation}" Width="150"/></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 ObservableCollection<Person> People { get; set; }public MainViewModel(){People = new ObservableCollection<Person>{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是如何关联起来的。

整体架构

代码主要包含以下几个关键部分:

  1. MainWindow.xaml:定义UI结构和绑定
  2. MainViewModel:提供数据和NameColumnWidth属性
  3. BindingProxy:解决DataContext绑定问题
  4. DataGridHelper:实现列宽绑定的附加属性
  5. DoubleToDataGridLengthConverter:类型转换器

数据流分析

1. ViewModel到Slider的绑定

<Slider Value="{Binding NameColumnWidth, Mode=TwoWay}" />
  • Slider的Value属性双向绑定到ViewModel的NameColumnWidth属性
  • 当用户拖动滑块时,NameColumnWidth会被更新
  • 同时,TextBlock显示当前宽度值也是绑定到同一属性

2. ViewModel到DataGrid列的绑定

这是最复杂的部分,涉及多层绑定:

a. 绑定代理(BindingProxy)
<local:BindingProxy x:Key="Proxy" 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. 数据流
  1. 用户拖动Slider → NameColumnWidth更新
  2. 由于Proxy.Data绑定到整个DataContext,Proxy能感知到变化
  3. BindableWidth属性通过Proxy获取到新的NameColumnWidth值
  4. DataGridHelper的OnBindableWidthChanged回调被触发
  5. 回调中将新的值赋给DataGridColumn.Width

关键机制详解

1. BindingProxy的作用

BindingProxy解决了DataGrid列定义中无法直接访问DataContext的问题:

  • DataGrid列不是可视化树的一部分,没有继承DataContext
  • 通过创建Proxy作为静态资源,绑定到当前DataContext
  • 然后在列绑定中通过Source={StaticResource Proxy}访问

2. DataGridHelper附加属性

这是实现列宽绑定的核心:

  1. 定义BindableWidth附加属性
  2. 当属性值变化时,OnBindableWidthChanged回调被触发
  3. 回调中将新值赋给DataGridColumn的Width属性
private static void OnBindableWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{if (d is DataGridColumn column){column.Width = (DataGridLength)e.NewValue;}
}

3. 数据关联路径

完整的绑定路径是:
Slider.ValueViewModel.NameColumnWidthProxy.Data.NameColumnWidthDataGridHelper.BindableWidthDataGridColumn.Width

为什么这样设计
  1. 解决DataContext问题:DataGrid列不在可视化树中,无法直接绑定到ViewModel
  2. 类型兼容:DataGridColumn.Width是DataGridLength类型,而Slider操作的是double
  3. 重用性:通过附加属性和代理,可以方便地在其他地方重用这种绑定方式

解决方案分析

问题涉及WPF中两个复杂的技术点:DataGridTemplateColumn的特殊绑定行为和属性变更通知机制。

核心问题分析

最初遇到的问题是由以下因素共同导致的:

  1. DataGridTemplateColumn不在可视化树中
    这导致它无法通过RelativeSourceElementName绑定到窗口或DataGrid的DataContext。

  2. Width属性类型不匹配
    DataGridColumn.Width属性类型是DataGridLength,直接绑定了double类型,需要类型转换。

  3. 列宽属性变更通知缺失
    即使绑定成功,DataGridTemplateColumnWidth属性默认不会自动响应绑定源的变化。

关键解决方案组件

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.Resources><local:DoubleToDataGridLengthConverter x:Key="DoubleToDataGridLengthConverter"/><local:BindingProxy x:Key="Proxy" Data="{Binding}"/>
</Window.Resources><DataGridTemplateColumn 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转换为DataGridLength
  • local:DataGridHelper.BindableWidth:使用附加属性而非直接设置Width

为什么这个方案有效

  1. 突破可视化树限制
    通过BindingProxy,我们将DataContext从资源树引入,避开了DataGridTemplateColumn不在可视化树中的问题。

  2. 类型安全转换
    转换器确保了从doubleDataGridLength的正确类型转换。

  3. 强制属性更新
    附加属性的PropertyChangedCallbackOnBindableWidthChanged)在值变化时主动更新列宽,解决了通知缺失问题。


文章转载自:

http://dtPtwViU.rcntx.cn
http://kPFLAxf7.rcntx.cn
http://3hnAK5w2.rcntx.cn
http://4Frvf6bq.rcntx.cn
http://0aRSm26o.rcntx.cn
http://fOsP7bv6.rcntx.cn
http://Obpcm8eC.rcntx.cn
http://OYfvNusY.rcntx.cn
http://UNhPVlFg.rcntx.cn
http://7knaQGxe.rcntx.cn
http://Ml9fHt1n.rcntx.cn
http://GvreaaaN.rcntx.cn
http://t9rhdsSN.rcntx.cn
http://AlLAHcpw.rcntx.cn
http://NIxpFrsY.rcntx.cn
http://MlDMmuvQ.rcntx.cn
http://LlE5cKzS.rcntx.cn
http://m72lsm3c.rcntx.cn
http://dzPrEOqC.rcntx.cn
http://bWFpVYNi.rcntx.cn
http://BRK09w6Y.rcntx.cn
http://7Ps75G4E.rcntx.cn
http://wdx67sFp.rcntx.cn
http://WS7j6B55.rcntx.cn
http://mvLyQSRO.rcntx.cn
http://aUGXxoJv.rcntx.cn
http://923wEbXE.rcntx.cn
http://i12dWUs7.rcntx.cn
http://MkPzfuS8.rcntx.cn
http://nmTm4Yko.rcntx.cn
http://www.dtcms.com/wzjs/733638.html

相关文章:

  • 自己做的网站图片加载过慢建设银行的官方网站公告
  • 青龙建站教程自学网北京网站建设itcask
  • 北京网站建设推广服wordpress滑块教程
  • 名师工作室网站建设现状调查深圳龙岗是不是很落后
  • 电力建设期刊网站经常维护吗quark搜索引擎入口
  • 茂名网站建设方案开发百度24小时人工电话
  • 台州网站建设哪家便宜wordpress post提交表单
  • 住房与城乡建设部网站打不开网站做多个语言有什么好处
  • 淘宝网站短链接怎么做个人备案网站做盈利合法吗
  • 个人网站建设目标广告设计制作服务方案
  • 深圳市网站建设制作设计平台常州做网站优化
  • 工业风 网站建设聊城专业网站建设公司电话
  • 网站文章怎么更新android开发环境搭建
  • 镇江市城市建设投资公司官方网站中国能建设计公司网站
  • 做网站 徐州wordpress 功能开发教程
  • 网站域名的建立wordpress 检测插件
  • 视觉传达毕业设计网站做网站为什么能挣钱
  • 肇庆制作网站软件深圳定制工作装
  • django做网站怎样网站页面的大小写
  • 做常识的网站大连市工程建设信息网
  • 旅游信息管理网站开发文件wordpress缩略图错乱
  • 潍坊高端网站建设国外做锅炉的网站
  • 无锡网站策划公司企业管理咨询机构
  • 网站建设与管理维护书籍网站快照历史
  • 建设邯郸网站百度收录要多久
  • 企业网站模板下载哪家好怎么用vps做网站
  • 大良网站建设基本流程高端网络培训课程
  • 深圳做企业网站的公司甘肃城乡建设厅网站首页
  • 中山模板建站代理网站空间到期怎么续费
  • 阿里云个人网站制作随便来一个公司邮箱