-1

i need to create a GUI-DLL-Component which should be used as a dialog. This dialog performs some calculations and then searches the database for the result (let's say the result is a number).

The result is bound to the view via a public property of the viewmodel.

The user want to intantiate an object of this GUI component and open the dialog, after the calculation is done the user need to access the result in a later point of time.

What i want to ask is how to access the (Result) public property of the viewmodel after intantiating the object because i don't know how to do it in a MVVM way. My temproral solution is to cast the data context of the window in code behind and then access its public property. But it's not MVVM (In this case the dialog is derived from window class. And after the method .showdialog() is called there is no way to access the public property of the window's viewmodel).

How can i do this in a MVVM manner?

Thank you very much for your help :).

Best regards,

Minh

Edit:

here is my code:

XAML:

<catel:DataWindow x:Class="WpfApplication3.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:catel="http://catel.codeplex.com" 
                  mc:Ignorable="d" 
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  xmlns:viewmodel="clr-namespace:WpfApplication3.ViewModels"
                  d:DesignHeight="273" 
                  d:DesignWidth="457" 
                  SizeToContent="WidthAndHeight">

    <Window.DataContext>
        <viewmodel:MainWindowViewModel></viewmodel:MainWindowViewModel>
    </Window.DataContext>    

    <Grid>
        <Button Content="Calc 1+1" 
                Height="39" 
                Name="button1" 
                Width="87" 
                Command="{Binding CalcCmd}"/>

        <TextBox Height="23" 
                 HorizontalAlignment="Left" 
                 Name="textBox1" 
                 VerticalAlignment="Top" 
                 Width="87" 
                 Margin="174,152,0,0" 
                 Text="{Binding Result}"/>

        <Label Content="Result:" 
               Height="28" 
               HorizontalAlignment="Left" 
               Margin="111,152,0,0" 
               Name="label1" 
               VerticalAlignment="Top" 
               Width="46" />
    </Grid>        
</catel:DataWindow>

Code Behind:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Catel.Windows;
using WpfApplication3.ViewModels;

namespace WpfApplication3
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : DataWindow
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(MainWindowViewModel mainWindowViewModel)
            : base(mainWindowViewModel)
        {
            InitializeComponent();
        }

    //Temporal solution
    public string Result
    {
        get {
            MainWindowViewModel vm = (MainWindowViewModel)this.DataContext;
            return vm.Result;
        }
    }
    }
}

ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Catel.MVVM;
using Catel.Data;

namespace WpfApplication3.ViewModels
{
    /// <summary>
    /// name view model.
    /// </summary>
    public class MainWindowViewModel : ViewModelBase
    {
        #region Fields
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        public MainWindowViewModel()
        {
            registeringCommands();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title { get { return "MyMainWindow"; } }

        /// <summary>
        /// Gets or sets the property value.
        /// </summary>
        public string Result
        {
            get { return GetValue<string>(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }

        /// <summary>
        /// Register the Result property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ResultProperty = 
            RegisterProperty("Result", typeof(string), null);

        #endregion

        #region Commands
        /// <summary>
        /// Gets the name command.
        /// </summary>
        public Command CalcCmd { get; private set; }

        /// <summary>
        /// Method to invoke when the name command is executed.
        /// </summary>
        private void execute_CalcCmd()
        {
            try {
                Result = (1 + 1).ToString();
            }
            catch(Exception ex)
            {
                throw;
                //log
            }
        }
        #endregion

        #region Methods
        private void registeringCommands()
        {
            CalcCmd = new Command(execute_CalcCmd);
        }
        #endregion
    }
}
Benjamin Martin
  • 576
  • 1
  • 8
  • 27
  • What have you done so far? Anything? – meilke Oct 04 '13 at 12:28
  • What i want to ask is how to access the (result) public property of the viewmodel after intantiating the object because i don't know how to do it in a MVVM way. My temproral solution is to cast the data context of the window in code behind and then access its public property. But it's not MVVM. – Benjamin Martin Oct 04 '13 at 12:33

1 Answers1

1

You can pass a view model instance to the ShowDialog method of the UIVisualizerService. This view model will still be available after the window is closed and this is the same view model used on the DataWindow. You can simply use the value in the calling view model.

If the result needs to be used in a lot of classes, it is better to create a dedicated class / service for this and register it in the ServiceLocator. For example, a Settings : ISettings which you modify in the DataWindow and you can read anywhere by querying the ServiceLocator / DependencyResolver or by letting them get injected in the classes where you need the information.

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32
  • Thx for the answer :). By the way i read your article about DataWindow: https://catelproject.atlassian.net/wiki/display/CTL36/DataWindow#DataWindow-UsingtheDataWindowinMVVM. In the code behind I got a xaml parser exception following the example without an empty constructor. Have i done something wrong? – Benjamin Martin Oct 04 '13 at 14:27