加载序列化视图模型并将其绑定到 WPF 窗口?

发布于 2024-08-25 09:19:03 字数 1460 浏览 5 评论 0原文

我正在为一个简单的 ETL 工具编写一个单窗口 UI。 UI 由窗口、窗口背后的代码、窗口的视图模型和业务逻辑组成。我想向用户提供保存 UI 状态的功能,因为大约 10-12 个文本框的内容将在会话之间重复使用,但特定于用户。我想我可以序列化视图模型,其中包含文本框中的所有数据,这工作正常,但我在将序列化 XML 文件中的信息加载回文本框中时遇到问题。

窗口的构造函数:

    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 相应的属性:

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

后面代码中的 Load 事件:

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

正如您所猜测的,LoadModel 方法反序列化用户驱动器上的序列化文件。

我在网上找不到关于这个问题的太多信息。我知道这可能与我的绑定有关。反序列化视图模型后,是否有某种方法可以刷新 XAML 上的绑定?或者也许刷新视图模型上的所有属性?或者我完全疯了,认为这一切都可以做到?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-09-01 09:19:03

假设您的 loadState_Click 事件位于后面的窗口代码上,您可以尝试此操作。

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

Assuming that your loadState_Click event is on the Window code behind you could try this.

private void loadState_Click(object sender, RoutedEventArgs e) 
{ 
    string statePath = this.getFilePath(); 
    this.DataContext = ViewModel.LoadModel(statePath); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文