标签:CheckBox return get ViewModel private GasInformation set WPF public
WPF listView下添加CheckBox的ViewModel触发事件
在开发中遇到需要在listView下添加CheckBox的选中直接触发对应的数据加载,而且是在ViewModel中操作,发现添加触发器并不生效。最后通过命令嵌套解决该问题
代码部分:
xaml:
<ListView.Resources>
</ListView.Resources>
<ListView.View>
<GridView.Columns>
<GridViewColumn.HeaderTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumn.CellTemplate>
</GridViewColumn.CellTemplate>
</GridView.Columns>
</ListView.View>
后台代码:
public class GasInformation : WpfBaseControlViewModel, IGasInformationViewModel
{
private bool isCheck;
private string gas;
private bool isDraw;
private string maxFlow;
///
/// GasInformation Filed
///
public bool IsCheck
{
get { return isCheck; }
set { Set(() => IsCheck, ref isCheck, value); }
}
///
/// GasInformation Filed
///
public string Gas
{
get { return gas; }
set { Set(() => Gas, ref gas, value); }
}
///
/// GasInformation Filed
///
public string MaxFlow
{
get { return maxFlow; }
set { Set(() => MaxFlow, ref maxFlow, value); }
}
///
/// GasInformation Filed
///
public bool IsDraw
{
get { return isDraw; }
set { Set(() => IsDraw, ref isDraw, value); }
}
///
///
///
public Action CallBackAction { get; set; }
///
/// Command to DrawRadioCommand of PartialPressureScreen
///
public ICommand CheckBoxCommand
{
get { return new DelegateCommand { CommandActionParam = ExecutionDrawRadioCommand }; ; }
}
private void ExecutionDrawRadioCommand(object obj)
{
CallBackAction?.Invoke();
}
}
对应接口实现:
GasDataWrapper.Add(new GasInformation { Gas = “SiCL4”,MaxFlow = “0” ,CallBackAction = callBackAction });
callBackAction 中操作对应的实际数据加载。
问题解决思路
开始的时候发现添加触发器一直无法生效,后来找到问题原因:因为当前操作的对象是在GasDataWrapper对象中,对应的触发器命令并不在该对象的类中存在该属性,自然无法触发。
之后处理问题,那么就在该对象中添加对应的事件进行触发。注册对应的事件:CheckBoxCommand,之后添加委托,然后使用事件CallBackAction 进行处理。
///
///
///
public Action CallBackAction { get; set; }
///
/// Command to DrawRadioCommand of PartialPressureScreen
///
public ICommand CheckBoxCommand
{
get { return new DelegateCommand { CommandActionParam = ExecutionDrawRadioCommand }; ; }
}
private void ExecutionDrawRadioCommand(object obj)
{
CallBackAction?.Invoke();
}
最后在接口的实现中加载该GasDataWrapper对象时,callBackAction()方法就是每一个对象点击CheckBox时进行触发的操作事件。listView中添加触发器不生效其实就是对象所属范围问题。需要格外注意。
标签:CheckBox,return,get,ViewModel,private,GasInformation,set,WPF,public 来源: https://blog.csdn.net/PassionLong/article/details/121309773
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。