这其中有难住我了,因为我以为我看着一切,但我一定是失去了一些东西。 我已经去了从MSDN杂志的传统MVVM模式:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
同时学习MVVM。 不过我通常复制的大部分代码,然后替换它,因为我需要,但今天我想从头开始构建的东西,看到可能有更多的它比我想象的。 MVVM出现时,我所使用的资源字典,而是用的DataContext直接做不绑定的工作。 这个问题最终要找到其他开发者使用建议结合他们发现。
问题的总结是这样的:为什么“的DataTemplate”在资源字典中似乎行不通如下而是直接“的DataContext”的方法,以期就为绑定呢?
难道是因为我做的代码混合身后后面的代码中设置的意见。 或可能是因为别的东西。 看来我的财产,它的实现是在资源字典中设置正确的视图模型,如果我把“DataContext的”直接在视图的XAML,但为什么不呢? 我认为方法的好处是,你可以设置一帮关系的一次。 我很好奇,如果有一些其他的设置需要做才能得到它的工作。 它是在MVVM方法的主要例子好奇他们使用的数据模板但它似乎还有更多的设置起来比我做的得到他们的“绑定”的工作。
哪些没有为我工作:
我试图做一些很基本的东西,在主窗口的XAML,留下我的一些代码出来,使其更简单:
主窗口XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" x:Class="WPFTesting12_2.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="Resources.xaml"/>
</Window.Resources>
<Grid>
<DockPanel x:Name="dockpanel">
<Menu DockPanel.Dock="Top" Height="30">
<MenuItem Header="Charting">
<MenuItem Header="MVVMDataBound" x:Name="mnuDataBoundSeriesMVVMCharting" Click="mnuDataBoundSeriesMVVMCharting_OnClick"/>
</MenuItem>
</Menu>
<TextBlock Height="5" Background="Black" DockPanel.Dock="Top" />
<DockPanel x:Name="dockchildren" DockPanel.Dock="Bottom"/>
</DockPanel>
</Grid>
</Window>
主窗口代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WindowState = WindowState.Maximized;
}
private void mnuDataBoundSeriesMVVMCharting_OnClick(object sender, RoutedEventArgs e)
{
View.DataBoundMVVMChart c = new DataBoundMVVMChart();
dockchildren.Children.Clear();
dockchildren.Children.Add(c);
}
}
}
资源词典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:WPFTesting12_2.View"
xmlns:vm="clr-namespace:WPFTesting12_2.ViewModel"
>
<DataTemplate DataType="{x:Type vm:DataBoundMVVMChartViewModel}">
<vw:DataBoundMVVMChart/>
</DataTemplate>
<Style TargetType="MenuItem">
<Setter Property="Background" Value="Wheat"/>
</Style>
<Style TargetType="Menu">
<Setter Property="Background" Value="Wheat"/>
</Style>
</ResourceDictionary>
查看DataBoundMVVMChart.xaml:
<UserControl x:Class="WPFTesting12_2.View.DataBoundMVVMChart"
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:WPFTesting12_2.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary Source="..\Resources.xaml"/>
</UserControl.Resources>
<Grid>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="TesterContent"/>
</Menu>
<Label DockPanel.Dock="Bottom" Width="300" x:Name="label" Height="50" Foreground="Blue" FontSize="24" Content="{Binding Path=HelloString}"/>
</DockPanel>
</Grid>
</UserControl>
视图模型绑定到上方的查看:
namespace WPFTesting12_2.ViewModel
{
class DataBoundMVVMChartViewModel : INotifyPropertyChanged
{
private string _HelloString;
public string HelloString
{
get { return _HelloString; }
set
{
_HelloString = value;
RaisePropertyChanged("HelloString");
}
}
public DataBoundMVVMChartViewModel()
{
HelloString = "Hello there from the ViewModel";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
好了,现在的结合将在视图中失败,但还没有颜色的资源进来,所以我想我做错了什么事,但后面的代码将获得产权了。 因此,让我们继续前进:
什么工作:
Magicially如果我只是添加这些四行的观点:
此添加到在顶部的“用户控件”部分的声明:
xmlns:local="clr-namespace:WPFTesting12_2.ViewModel"
然后设置为用户控件的DataContext的一个参考:
<UserControl.DataContext>
<local:DataBoundMVVMChartViewModel/>
</UserControl.DataContext>