首页 新闻 会员 周边

WPF MVVM 事件 不触发

0
悬赏园豆:10 [已关闭问题] 关闭于 2016-11-01 08:23

CS:

public partial class frmControlMenu : ModernWindow
{
public frmControlMenu(string siteGb, bool printGb, bool msrGb)
{
InitializeComponent();
this.DataContext = new frmControlMenuVM(siteGb, printGb, msrGb);
}

private void frmControlMenu_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (((Dz.POS.ViewModel.frmControlMenuVM)this.DataContext).Control == Visibility.Collapsed)
{
this.DialogResult = true;
this.Close();
}
}
}

VM:

using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dz.POS.ViewModel
{
public class frmControlMenuVM : ViewModelBase
{
public bool bPrint = false;
// choisw 20131114 
public bool bMsrGb = false;

private Microsoft.Win32.RegistryKey KeyBasic; 
#region 事件
public DelegateCommand btnReserveCommand { get; set; }
public DelegateCommand btnTakeCommand { get; set; }
public DelegateCommand btnLiquidationCommand { get; set; }
public DelegateCommand btnCloseCommand { get; set; }
public DelegateCommand btnExitCommand { get; set; }
#region 关闭窗口
public System.Windows.Visibility _control { get; set; }
public System.Windows.Visibility Control
{
get { return _control; }
set
{
_control = value;
InitPropertyChanged();
}
}
#endregion
#endregion

public frmControlMenuVM(string siteGb, bool printGb, bool msrGb)
{
Control = System.Windows.Visibility.Visible;
#region 注册事件
this.btnReserveCommand = new DelegateCommand();
this.btnReserveCommand.ExecuteCommand = new Action<object>(this.btnReserve_Click);
this.btnTakeCommand = new DelegateCommand();
this.btnTakeCommand.ExecuteCommand = new Action<object>(this.btnTake_Click);
this.btnLiquidationCommand = new DelegateCommand();
this.btnLiquidationCommand.ExecuteCommand = new Action<object>(this.btnLiquidation_Click);
this.btnCloseCommand = new DelegateCommand();
this.btnCloseCommand.ExecuteCommand = new Action<object>(this.btnClose_Click);
this.btnExitCommand = new DelegateCommand();
this.btnExitCommand.ExecuteCommand = new Action<object>(this.btnExit_Click);
#endregion
bPrint = printGb;
bMsrGb = msrGb;
}

#region 管理菜单按钮
public void btnReserve_Click(object obj)
{

}
public void btnTake_Click(object obj)
{

}

public void btnLiquidation_Click(object obj)
{
Control = System.Windows.Visibility.Collapsed;
}

public void btnClose_Click(object obj)
{
InitPropertyChanged();
Control = System.Windows.Visibility.Collapsed;
}

public void btnExit_Click(object obj)
{
try
{
string msgData = CommonHelper.CallMsgPopUp("Question", CommonManager.GetMessage("30379"), "", "2", ""); 
if (msgData == "NO")
{
return;
}

KeyBasic = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\SK.OFSPOS", true); 

try
{
if (this.FormInterface.GetCurrentUser() != null)
{
Common.Data.CoreData data = CommonManager.HistoryLogin(this.FormInterface.GetCurrentUser().BusinessUnitID, this.FormInterface.GetCurrentUser().UserId, string.Empty, CommonManager.InvokeState.Logout.ToString());
}
}
catch { }

Environment.Exit(0);
}
catch (Exception ex)
{

string msg = Utility.UtilityHelper.GetFinalInnerException(ex).Message;
Utility.UtilityHelper.WriteLog("frmControlMenu " + msg, Utility.UtilityHelper.eLogCategory.UI, System.Diagnostics.TraceEventType.Error);
Environment.Exit(0);
}
}
#endregion
}
}

VIEW 

<mui:ModernWindow x:Class="Dz.POS.POP.UI.frmControlMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
Width="540"
Height="300"
Title="管理菜单"
Visibility="{Binding Control}"
IsVisibleChanged="frmControlMenu_IsVisibleChanged"
Style="{StaticResource BlankWindow}">
<Border Margin="0,0,0,0">
<Grid>
<Button Width="120" Height="80" Margin="50,10,350,100" Content="储备金登记" Command="{Binding btnReserveCommand}">
</Button>
<Button Width="120" Height="80" Margin="175,10,150,100" Content="取款登记" Command="{Binding btnTakeCommand}">
</Button>
<Button Width="120" Height="80" Margin="350,10,0,100" Content="清算" Command="{Binding btnLiquidationCommand}">
</Button>
<Button Width="120" Height="80" Margin="50,100,350,10">
</Button>
<Button Width="120" Height="80" Margin="175,100,150,10" Content="关闭" Command="{Binding btnCloseCommand}">
</Button>
<Button Width="120" Height="80" Margin="350,100,0,10" Content="结束" Command="{Binding btnExitCommand}">
</Button>
</Grid>
</Border>
</mui:ModernWindow>

点击关闭按钮不触发 frmControlMenu_IsVisibleChanged

问题补充:

ViewModelBase:

public partial class ViewModelBase : INotifyPropertyChanged, IDisposable
{
public ViewModelBase()
{
FormInterface = Common.UIInterface.GetUIInterface();
}
public virtual string DisplayName { get; protected set; }
protected void InitPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}

protected void OnPropertyChanged<T>(Expression<Func<T>> action)
{
var propertyName = GetPropertyName(action);
OnPropertyChanged(propertyName);
}
private static string GetPropertyName<T>(Expression<Func<T>> action)
{
var expression = (MemberExpression)action.Body;
var propertyName = expression.Member.Name;
return propertyName;
}
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
public event PropertyChangedEventHandler PropertyChanged;

public void Dispose()
{
this.OnDispose();
}


protected virtual void OnDispose()
{

}

#region Member Property
private Interfaces.IUserInterface _UIInterface;
#endregion

public Interfaces.IUserInterface FormInterface
{
get
{
return this._UIInterface;
}
set
{
this._UIInterface = value;
}
}

private Jc.Security.UserInfoContext _userInfoCtx = null;

public Jc.Security.UserInfoContext UserInfoCtx
{
get { return _userInfoCtx; }
set { _userInfoCtx = value; }
}
}

愤青愤青的主页 愤青愤青 | 初学一级 | 园豆:4
提问于:2016-10-24 11:37
< >
分享
所有回答(1)
0

命令这样试用根本就不是MVVM,你这后台跟界面的代码几乎没有分离,命令都需要继承ICommand接口的,DataContext指定到后台的ViewModel,前端界面Binding,如果需要传递参数一定注意他的AncestorType

东秦男人 | 园豆:240 (菜鸟二级) | 2016-11-29 11:17

 上来就按照自己的想法瞎猜

活在自己的世界里
支持(0) 反对(0) 愤青愤青 | 园豆:4 (初学一级) | 2016-11-29 21:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册