0

I'd like to have a simple dialog box with text field. For now I have:

ViewModel:

   public class MyViewModel
   {
       public string AA { get; set; }
   }

View:

public partial class MyView : Window
{

    public MyView(MyViewModel context)
    {
        InitializeComponent();
        this.DataContext = context;
    }

    public string BB {get; set; }
 }

 <Window 
        ...>
      <Grid>

            <Label>name:</Label>
            <TextBox Width="110">
                <TextBox.Text>
                    <Binding Path="AA"/>
                </TextBox.Text>
            </TextBox>
            ...
       </Grid >
 </Window>

In order to open it and get the value I call:

   MyViewModel model = new MyViewModel();
   MyView dialog = new MyView(model);
   dialog.ShowDialog();

Now I'm able to take the provided TextBox value from the MyViewModel AA property.

Is it possible to store the provided value in the MyView BB property (and so remove the MyViewModel)? I tried to change the xaml configuration but no mater what I put there the BB property is always null.

The only reason I ask is that my dialog will do nothing more than collect this one single value and I'd like to keep it as thin as possible.

3
  • You should wrap that dialog in a using block Commented Jan 22, 2015 at 16:57
  • @phadaphunk, As MyView is a Window from WPF, it has no Dispose method. Calling ShowDialog should be enough for Close to get called and clean up. Commented Jan 22, 2015 at 17:07
  • You mean the older winforms way of returning a value from a dialog, like this? Commented Jan 22, 2015 at 19:41

3 Answers 3

0
 <Window x:Name="root"
        ...>
      <Grid>

            <Label>name:</Label>
            <TextBox Width="110" Text="{Binding BB, ElementName=root} />
            ...
       </Grid >
 </Window>

should be all you need to do.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also simply replace

    this.DataContext = context;

by

    this.DataContext = this;

and i would put it before the InitializeComponent call.

But if you want to store the value from textbox to BB, then you also need to set the mode:

    <TextBox Text="{Binding BB, Mode=TwoWay}"/>

And don't forget to implement INotifyPropertyChanged in your MyView class.

Comments

0

you could take the aprroach i use: Good or bad practice for Dialogs in wpf with MVVM?

here you call a dailogservice like

var result = this.uiDialogService.ShowDialog("Dialogwindow title goes here", dialogwindowVM);

... do anything with the dialog result...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.