开发者

Dont close window if the validation fails - in WPF

I am beginner in WPF and MVVM. I have a simple wpf window in which i am adding 2 values. and i am using command binding in add button.

here is my xaml code for button

<Button Content="OK" Name="btn_OK" Command="{Binding AddShutterType}" />

This command is written in my view model, and also i am doing some validations But my problem is if validation fails or succeeds my window is not closing!! If i give "this.close" window button click event, then it always closes. My requirement is to retain the window if validation fails and close if validation succeeds. How to do this?

Here 开发者_StackOverflow中文版is my view model code which contains validation part.

    private ICommand _AddShutterType;

    public ICommand AddShutterType
    {
        get
        {
            if (_AddShutterType == null)
            {
                _AddShutterType = new DeligateCommand.DelegateCommand(delegate()
                {
                    ShutterNameToAdd.Trim();
                    ShutterCodeToAdd.Trim();

                    StringBuilder SB = new StringBuilder();
                    if (ShutterCodeToAdd == "")
                    {
                        SB.Remove(0, SB.Length);
                        SB.Append("Please type in a Code for the shutter.");
                        throw new ArgumentException(SB.ToString());
                    }

                    if (ShutterCodeToAdd.Length > 10)
                    {
                        SB.Remove(0, SB.Length);
                        SB.Append("Shutter type code size cannot be more than 5");
                        throw new ArgumentException(SB.ToString());
                    }

                    if (ShutterNameToAdd == "")
                    {
                        SB.Remove(0, SB.Length);
                        SB.Append("Please type in a Name for the shutter.");
                        throw new ArgumentException(SB.ToString());
                    }                      

                   Model.AddShutterType(ShutterCodeToAdd, ShutterNameToAdd);
                });
            }
            return _AddShutterType;
        }
    }

Please any one help me..


My solution is to hold a ref of the view in the vm, but it will be an interface;

eg.

public View : UserControl, IView
{
    void IView.Close()
    { 
        this.Close();
    }
}

public ViewModel
{
    public IView View{get;set;}

    public void CommandImpl()
    {
        if (Validated())
            View.Close();
    }
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜