【问题标题】:How do you use a DataTemplate defined in Application.Resources?如何使用 Application.Resources 中定义的 DataTemplate?
【发布时间】:2015-03-15 17:43:51
【问题描述】:

如果我的窗口无法访问其中定义的资源,Application.Resources 的用途是什么?

这行得通,我得到一个窗口,里面有一个文本框,里面写着“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,但是以某种方式显示的文本只是说我的视图模型类的名称“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>

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?

【问题讨论】:

  • 你试过ContentControl而不是ContentPresenter吗?
  • 我刚试了一下,使用 ContentControl 或 ContentPresenter 似乎没有任何区别。

标签: c# wpf xaml mvvm


【解决方案1】:

将您的数据模板添加到字典中。它需要具有应用程序资源应该具有的默认样式。有关更多说明,请参阅链接。 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>

  <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>

【讨论】:

  • 为什么在 Application.Resources 中定义 DataTemplate 不起作用,但在单独的文件中定义它并合并它确实有效?这对我来说毫无意义。
  • 查看此链接以获取解释。 stackoverflow.com/questions/4813488/…
  • 所以......它应该按照我的方式工作,但这是一个错误。我想这会让我感觉不那么愚蠢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 2011-02-06
  • 1970-01-01
  • 2019-10-10
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多