Loading and binding a serialized view model to a WPF window?

Posted by generalt on Stack Overflow See other posts from Stack Overflow or by generalt
Published on 2010-03-17T02:01:15Z Indexed on 2010/03/17 2:01 UTC
Read the original article Hit count: 383

Filed under:
|
|
|

Hello all.

I'm writing a one-window UI for a simple ETL tool. The UI consists of the window, the code behind for the window, a view model for the window, and the business logic. I wanted to provide functionality to the users to save the state of the UI because the content of about 10-12 text boxes will be reused between sessions, but are specific to the user. I figured I could serialize the view model, which contains all the data from the textboxes, and this works fine, but I'm having trouble loading the information in the serialized XML file back into the text boxes.

Constructor of window:

    public ETLWindow()
    {
        InitializeComponent();

        _viewModel = new ViewModel();

        this.DataContext = _viewModel;
        _viewModel.State = Constants.STATE_IDLE;

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

XAML:

<TextBox x:Name="targetDirectory" 
         IsReadOnly="true"
         Text="{Binding TargetDatabaseDirectory, UpdateSourceTrigger=PropertyChanged}"/>

ViewModel corresponding property:

    private string _targetDatabaseDirectory;
    [XmlElement()]
    public string TargetDatabaseDirectory
    { 
        get { return _targetDatabaseDirectory; }
        set { _targetDatabaseDirectory = value; OnPropertyChanged(DataUtilities.General.Utilities.GetPropertyName(() => new ViewModel().TargetDatabaseDirectory)); }

Load event in code behind:

    private void loadState_Click(object sender, RoutedEventArgs e)
    {
        string statePath = this.getFilePath();
        _viewModel = ViewModel.LoadModel(statePath);
    }

As you can guess, the LoadModel method deserializes the serialized file on the user's drive.

I couldn't find much on the web regarding this issue. I know this probably has something to do with my bindings. Is there some way to refresh on the bindings on the XAML after I deserialize the view model? Or perhaps refresh all properties on the view model? Or am I completely insane thinking any of this could be done?

Thanks.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding