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

Nic*_*ick 2 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

Ayy*_*ian 6

将datatemplate添加到字典中.它需要具有应用程序资源应具有的默认样式.请参阅链接以获取更多解释.app.xaml中的datatemplate在没有任何样式的情况下没有被拾取?

在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>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

要么

  <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>
Run Code Online (Sandbox Code Playgroud)
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();
    }
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 为什么在Application.Resources内部定义DataTemplate无效,但在单独的文件中定义并将其合并却有效?这对我完全没有意义。 (2认同)