WPF的一些控件的触发事件记录

<c1:C1ComboBox Width="230" ItemsSource="{Binding ReplaceWayList}" Style="{StaticResource ListSearch-C1ComboBox}" SelectedValueChanged="C1ComboBox_SelectedValueChanged",

下拉框事件,SelectedValueChanged选中值改变事件,通过这个事件触发相关操作

private void C1ComboBox_SelectedValueChanged(object sender, C1.WPF.PropertyChangedEventArgs<object> e)

{

var vm = DataContext as PlateReplaceApplyFormVM;

if (vm == null)

{

return;

}

//也可以直接通过控件名获取,但是表格中重复的时候就不能这样写ImpCouponMode就是控件名称

// string couponMode = ImpCouponMode.Text;

//获取旧值,也可以获取新值,因为我这边要检测改变情况清空的数据,没有获取新值,只要有旧值说明之前应该选中值写过数据了,更换就需要清空

var OldValue = e.OldValue;

if (OldValue!=null)

{

vm.deleteAftrePartChange();

}

}

控件的触发值改变事件有三种、SelectedValueChanged、SelectedItemChanged、SelectedIndexChanged

我一般用前面两种SelectedValueChanged,可以直接获取变化的值,可以获取到新旧值,SelectedItemChanged是获取选中对象里面会有很多属性,可以将选中的对象转换为具体的对象var a = sender as C1ComboBox;就是直接转控件,通过控件的SelectedItem获取选中项,选中项在转换为具体的对象,通过属性key获取值,进行判断,也可以用于值的更改

<c1:C1ComboBox Style="{StaticResource Grid-ComboBox-Nums}" ItemsSource="{Binding DisposalTypeList}" IsEnabled="{Binding IsEnable}"

DisplayMemberPath="Value" SelectedValuePath="Key" Name="c1cbCheckoutType"

SelectedValue="{Binding Item.DisposalType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True,

NotifyOnValidationError=True}" Width="90" SelectedItemChanged="c1cbCheckoutType_SelectedItemChanged"/>

private void c1cbCheckoutType_SelectedItemChanged(object sender, C1.WPF.PropertyChangedEventArgs<object> e)

{

//选中的控件项转化成枚举项对象e.NewValue到Common.StatusEnum.ComboxKeyValue

var a = sender as C1ComboBox;

if (null != a && a.SelectedIndex == -1) return;

if (null != ((Common.StatusEnum.ComboxKeyValue)a.SelectedItem))

{

_vm = DataContext as AssetDisposalDetailFormVM;

// 选出售显示的控件

if (((Common.StatusEnum.ComboxKeyValue)e.NewValue).Key == "1")

{

spCheckoutType.Visibility = Visibility.Visible;

spIsContract.Visibility = Visibility.Visible;

spWr.Visibility = Visibility.Visible;

spPhone.Visibility = Visibility.Visible;

lbWrName.Content = "回收商";

}

else

{

spCheckoutType.Visibility = Visibility.Hidden;

spIsContract.Visibility = Visibility.Collapsed;

_vm.Item.TaxRate = null;

spWr.Visibility = Visibility.Collapsed;

spPhone.Visibility = Visibility.Collapsed;

}

}

// 利用控件的值指回旧值 CbBox.SelectedItem = e.OldValue;

var CbBox = sender as C1ComboBox;

var newItem = e.NewValue as KeyValue;

if (CbBox == null || newItem == null || _vm == null)

{

return;

}

if ((newItem.Key == (int)PaymentMethodEnum.Acceptance || newItem.Key == (int)PaymentMethodEnum.Check) && _vm.SupBankIsEmpty)

{

MessageBoxUtil.ShowWarn("该物流商无银行账号,支付方式只能选"转账"或"现金"!");

CbBox.SelectedItem = e.OldValue;

}

还有表格中通过点击事件获取整行数据

Click="BtnMaterialCode_Click"

private void BtnMaterialCode_Click(object sender, RoutedEventArgs e)

{

var vm = DataContext as StockClassParamChangeApplyFormVM;

if (vm == null)

{

return;

}

var testInfo = CfgReviewItem.HitTest(Mouse.GetPosition(CfgReviewItem));

if (testInfo.Row > -1)

{

var selectedItem = CfgReviewItem.RowstestInfo.Row.DataItem as StockClassParamChangeDetailLogModel;

vm.CurrentDetailItem = selectedItem;

vm.SelectMaterialCode();

}

}

还有复选框的选中Checked和不选中Unchecked事件,都是同样的处理方式

<CheckBox IsChecked="{Binding Item.IsOutward}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Style="{StaticResource Grid-CheckBox}" Height="15" Width="20"/>

private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)

{

var vm = DataContext as ProcedureScrapFormVM;

if (vm == null)

{

return;

}

vm.SetOutProcedureScrapGroupList();

}

相关推荐
samble3 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble12 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴13 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67514 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教14 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨16 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟17 天前
WPF(数据绑定)
wpf
samble18 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟18 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911121 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理