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.Rows[testInfo.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();
}