0

I saw a lot of different information on this theme but nothing helped me.

  1. How do u think? Probably better use a simpler pattern than MVVM with OpenFileDialog?
  2. I have 2 functions Ok and Cancel. Each one has (this.DialogResult = true) or (this.DialogResult = false). I must make it in MVVM pattern. I am using value -> (bool cls = true) right now and bind it to DialogResult = {Binding cls} and change this property in different functions but it doesn't work. I get some exception.

Do you know how I can solve this problem with closing OpenFileDialog?

Adam
  • 15,537
  • 2
  • 42
  • 63
Thomas Wingfield
  • 95
  • 1
  • 3
  • 7

2 Answers2

0

create as method here is something that may get you along the lines of what you are looking for

    void OpenDialogAction(object param)
    {
        //Add code here
        OpenFileDialog d = new OpenFileDialog();


        if (d.ShowDialog() == true)
        {
            //set your variable true
        }
        else
        {
            //set your variable false
        }
    }
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

If you're OK and CANCEL buttons are always enabled regardless of state, I see little value in incorporating them in the MVVM design pattern. I don't do silverlight but assuming it's similar to WPF, you can check the IsCancel property in the Properties pane for your Cancel button and that will automatically close the dialog and set DialogResult=false when you click the button. For your OK button add something simple like this

    <Button ...Your normal settings here and then...
            Click="DoneButtonClickHandler">

Then in the code behind.

    private void DoneButtonClickHandler(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
    }

I know some purists want nothing in the code behind but to me, simpler more maintainable code is better (and the point of implementing MVVM). Now if my OK and Cancel buttons only enabled under certain model conditions I would probably set them up to interact with the ModelView.

Tod
  • 8,192
  • 5
  • 52
  • 93