关于WPF+Stylet架构的按钮command事件绑定问题!
现有一项目用到了WPF+Winform 相互显示
winform中利用ElementHost控件嵌入一个wpf的usercontrol,
usercontrol中有个button按钮,利用stylet框架,在usercontrolViewModel中有个buttonclick方法
现在调试一直提示“这可能意味着控件没有从父级继承它,例如,因为ContextMenu或Popup位于可视树中。您将需要显式设置“s:View.ActionTarget”。有关详细信息,请参阅wiki部分的“操作””
同样的代码用于wpf窗体上就没问题,但是在usercontrol就提示错误,
相关代码如下:
winform#
public WindowsForm1()
{
InitializeComponent();
this.Owner = WindowsControl.wcp;
UserControl1View cw = new UserControl1View(); //实例化自定义控件
cw.DataContext = new UserControl1ViewModel();//将UserControl1View的DataContext指向UserControl1ViewModel
elementHost1.Child = cw;//设置ElementHost所承载的WPF
this.Controls.Add(elementHost1);
}
UserControl1View#
<UserControl x:Class="PLC_CentralControlSystem.Pages.UserControl1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PLC_CentralControlSystem.Pages"
xmlns:controls="clr-namespace:HslCommunication.Controls;assembly=HslCommunication"
mc:Ignorable="d"
xmlns:s="https://github.com/canton7/Stylet"
d:DataContext="{d:DesignInstance local:UserControl1ViewModel}">
<Button x:Name="button_Upload" Content="上传数据" Command="{s:Action uploadclick}" /> //uploadclick是UserControl1ViewModel的一个自定义方法
UserControl1ViewModel#
public class UserControl1ViewModel : Screen
{
public string banxin { get; set; } = "0.0";//这几个变量跟前台视图的lable绑定,均无异常。
public string biaomian { get; set; } = "0.1";
public string pingjun { get; set; } = "1.0";
public string huanjing { get; set; } = "0.0";
public string sheding { get; set; } = "0.0";
public void uploadclick()//视图中按钮command的事件
{
huanjing = "100";
sheding = "200";
}
}
现在调试程序,前台lable的content没问题,初始值都是viewmodel中所定义的。
只有点击按钮时候,无法找到uploadclick()