多个ViewModel问题 [英] Multiple ViewModel problem

查看:185
本文介绍了多个ViewModel问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个带有xml的ViewModell绑定到我的视图中,如下所示:



I would like to bind a ViewModell with an xml to my view, like this one:

class ViewModel : INotifyPropertyChanged
       {
           public XmlDataProvider Dataprov { get; set; }


           private string _CommonProperty;

           public string CommonProperty
           {
               get { return _CommonProperty; }
               set { _CommonProperty = value; OnPropertyChanged(); }
           }

           ...
       }





我以这种方式将viewmodel添加到datacontext:





I added the viewmodel to datacontext in this way:

public MainWindow()
        {
            InitializeComponent();
            vm = new ViewModel();
            vm.Dataprov = new XmlDataProvider();
            try
            {
                vm.Dataprov.Source = new Uri("Test.xml",UriKind.Relative);
            }
            catch (Exception ex)
            {}
            this.DataContext = vm;
        }





然后我尝试了多种方法在我的视图中获取xml数据,但每次尝试都失败了:(



例如:



Then I tried multiple ways to get xml data in my view, but every tries failed :(

for example:

 <Grid>
        
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding  Source=Dataprov, XPath='Root/test1/test11'}" ></Label>
        </StackPanel>
</Grid>





如果XmlDataProvider是ViewModel的一部分,是否有任何可能的方法来使用xml绑定? />


谢谢



Is there any possible way to use xml binding, if the XmlDataProvider is part of the ViewModel?

Thank you

推荐答案





供您参考:

链接 [ ^ ]


我找到了解决此问题的解决方案。 XmlDataProvider已从viewmodel中删除,并被提供给资源:

I have found a walkaround solution for this issue. The XmlDataProvider was removed from the viewmodel,and was given to the resources:
<window.resources>
   <XmlDataProvider x:Key="Lang" Source="Test.xml" XPath="Root/test1"  />
</window.resources>



让我们说viewmodel是这样的:


Lets say the viewmodel like this:

class ViewModel : INotifyPropertyChanged 
{
  private bool _PropEnabled;

  public bool PropEnabled
  {
    get { return _PropEnabled; }
    set { _PropEnabled = value; OnPropertyChanged(); }
    ...
  }
}



现在可以完成绑定:


Now the binding could be accomplished:

<Label Content="{Binding Source={StaticResource ResourceKey=Lang}, XPath='test11'}" IsEnabled="{Binding PropEnabled}" ></Label>



因此,XML和视图可以同时用于绑定。 内容绑定到XML,但 IsEnabled 属性绑定到viewmodel。

我必须强调这只是我的问题的解决方法,但如何合并多个视图模型对我来说仍然是不洁净的。


So the XML and the view can be use for binding in the same time. The Content is binded to the XML, but the IsEnabled property is binded to the viewmodel.
I have to highlight this is only a workaround for my problem, but how to merge multiple viewmodels is still unclean for me.


这篇关于多个ViewModel问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆