关于WPF+Stylet架构的按钮command事件绑定问题!

tj_libo 2019-03-20 09:48:59
现有一项目用到了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()
...全文
896 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tj_libo 2019-03-22
  • 打赏
  • 举报
回复
问题已经解决,在view中定义了d:DataContext="{d:DesignInstance local:UserControl1ViewModel}" 所以在button的s:View.ActionTarget="{Binding}" 只需要binding即可,不用指定path
tj_libo 2019-03-20
  • 打赏
  • 举报
回复
求大神帮忙!!!!!!!!!
tj_libo 2019-03-20
  • 打赏
  • 举报
回复
前台button通过{s:Action uploadclick} 直接绑定viewmodel的uploadclick
tj_libo 2019-03-20
  • 打赏
  • 举报
回复
引用 1 楼 OrdinaryCoder 的回复:

  public DelegateCommand AlarmConfigCommand { get; set; }
   <Button Grid.Column="0" Content="确定" Width="90" Height="25"  Command="{Binding AlarmConfigCommand}"/>
我一般绑command都是绑委托,你可以这么试试
我用的是wpf的stylet轻量级mvvm架构,可直接绑定command。我会尝试一下你的方法
OrdinaryCoder 2019-03-20
  • 打赏
  • 举报
回复

public DelegateCommand AlarmConfigCommand { get; set; }
<Button Grid.Column="0" Content="确定" Width="90" Height="25" Command="{Binding AlarmConfigCommand}"/>

我一般绑command都是绑委托,你可以这么试试
exception92 2019-03-20
  • 打赏
  • 举报
回复
Command="{s:Action uploadclick}" -》s:Action 是个什么东西?能理解么,uploadclick在viewmodel中定义为ICommand命令形式,前台直接: Command="{Binding uploadclick}" 即可,不过你使用什么轻量级的xmlns:s="https://github.com/canton7/Stylet" 这个东西,就好好看看还需要哪里需要设置什么吧

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧