我在wpf应用程序中有一个视图模型,它需要高度使用XML。为此,我使用XMLDataProvider还有一些其他属性不依赖于XML。
所以我的viewmodel看起来像这样:
public class ViewModel : ViewModelBase
{
private XmlDocument _xmlDataProvider;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
base.RaisePropertyChangedEvent("Name");
}
}
public XmlDocument XmlDataProvider
{
get { return _xmlDataProvider; }
set
{
_xmlDataProvider = value;
base.RaisePropertyChangedEvent("XmlDataProvider");
}
}
}
我的XAML代码如下所示:
<UserControl x:Name="ctrlTemplate" x:Class= "CtrlTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFControl.UI"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:xckt="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
mc:Ignorable="d"
DataContext="{DynamicResource ViewModel}">
<UserControl.Resources>
<local:ViewModel x:Key="ViewModel" />
</ResourceDictionary>
</UserControl.Resources>
我想将xml提供程序设置为内部网格,如下所示:
<Grid x:Name="grdFill" Margin="0" RenderTransformOrigin="0.502,0.492" HorizontalAlignment="Left">
<Grid.DataContext>
<XmlDataProvider x:Name="template" Document="{Binding XmlDataProvider}"/> **This is not possible as Document is not a Dependency property**
</Grid.DataContext>
<StackPanel>
<Button ="{Binding XPath=Some Xpath}"/>
<Button Content="{Binding Name, Source={StaticResource ViewModel}}"/>
</StackPanel>
</Grid>
所以我的问题是:
如何在不违反MVVM规则的情况下设置XmlDataProvider的文档属性?
我也使用命令代替事件,但文档属性在ViewModel中不可用。