你如何使用Application.Resources中定义的DataTemplate?

时间:2015-03-15 17:43:51

标签: c# wpf xaml mvvm

如果我的Windows无法访问其中定义的资源,Application.Resources的目的是什么?

这很有效,我得到一个带有TextBox的窗口,上面写着" Loki"在里面...

App.xaml.cs:

public partial class App : Application
{
  protected override void OnStartup(StartupEventArgs e)
  {
    base.OnStartup(e);

    ViewModel.ViewModel1 oVM = new ViewModel.ViewModel1 { Name = "Loki" };
    MainWindow oVW = new MainWindow { Content = oVM };

    oVW.ShowDialog();
  }
}

MainWindow.xaml

<Window x:Class="TableGenerator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:TableGenerator.ViewModel"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <DataTemplate DataType="{x:Type vm:ViewModel1}">
      <TextBox Text="{Binding Path=Name}" />
    </DataTemplate>
  </Window.Resources>
  <ContentPresenter />
</Window>

但是将DataTemplate移动到Application.Resources而不是Window.Resources不起作用。当我运行这个时,我得到一个窗口,根本没有TextBox,但是有一些文本显示只是说我的viewmodel类的名称,&#34; TableGenerator.ViewModel.ViewModel1&#34;。

App.xaml.cs没有变化。

MainWindow.xaml更改为:

<Window x:Class="TableGenerator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <ContentPresenter />
</Window>

的App.xaml:

<Application x:Class="TableGenerator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:TableGenerator.ViewModel">
  <Application.Resources>
    <DataTemplate DataType="{x:Type vm:ViewModel1}">
      <TextBox Text="{Binding Path=Name}" />
    </DataTemplate>      
  </Application.Resources>
</Application>

为什么不在Application.Resources中查找我的DataTemplate?

1 个答案:

答案 0 :(得分:5)

将您的datatemplate添加到字典中。它需要具有应用程序资源应具有的默认样式。请参阅链接以获取更多解释。 datatemplate in app.xaml is not getting picked up without any styles?

  

在XAML中创建每个对象时,如果存在默认样式(即带有Type的键的样式),则应该应用该样式。您可以想象,有几种性能优化可以使(隐含)查找尽可能轻量级。

     

其中一个是我们不会查看资源字典,除非它们被标记为“包含默认样式”。有一个错误:如果所有默认样式都嵌套在合并的字典中三个级别(或更深),则顶级字典不会被标记,因此搜索会跳过它。解决方法是将默认样式放在根词典中的任何内容中。

然后参考以下代码。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:vm="clr-namespace:SQ15Mar2015_Learning">
<DataTemplate DataType="{x:Type vm:ViewModel}">
    <DockPanel>
        <TextBox Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}">
        </TextBox>
    </DockPanel>
</DataTemplate>

<Application x:Class="SQ15Mar2015_Learning.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:SQ15Mar2015_Learning">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

OR

  <Application.Resources>
    <DataTemplate DataType="{x:Type vm:ViewModel}">
        <DockPanel>
            <TextBox Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}">
            </TextBox>
        </DockPanel>
    </DataTemplate>
    <Style TargetType="{x:Type Rectangle}" />
</Application.Resources>
class ViewModel : INotifyPropertyChanged
{
    private string myVar;
    public string Name
    {
        get { return myVar; }
        set
        {
            if (value != myVar)
            {
                myVar = value;
                OnPropertyChanged("Name");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ViewModel oVM = new ViewModel { Name = "Loki" };
        MainWindow oVW = new MainWindow();
        oVW.DataContext = oVM;
        oVW.ShowDialog();
    }
}
<Window x:Class="SQ15Mar2015_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:SQ15Mar2015_Learning"
    Title="MainWindow" Height="350" Width="525" >

    <Grid>
        <ContentControl Content="{Binding }" />
    </Grid>
</Window>