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();

}

相关推荐
2601_9621741718 小时前
Redis 简介
数据库·redis·wpf
SamChan902 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan902 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience2 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者2 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars3 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan903 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk3 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080164 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf
hh9505 天前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区