首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用在DataTemplate中定义的Application.Resources?

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

Stack Overflow用户
提问于 2015-03-15 17:43:51
回答 1查看 6K关注 0票数 3

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

这个工作,我得到一个窗口与一个TextBox,上面写着“洛基”在里面.

App.xaml.cs:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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更改为:

代码语言:javascript
运行
复制
<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:

代码语言:javascript
运行
复制
<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呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-15 19:06:03

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

在创建XAML中的每个对象时,如果存在默认样式(即样式w/类型键),则应该应用该样式。正如您可以想象的那样,有几个性能优化可以使(隐含的)查找尽可能地轻量化。 其中之一是我们不会查看资源字典,除非它们被标记为“包含默认样式”。有一个bug:如果所有默认样式都嵌套在合并字典中,那么顶级字典就不会被标记,所以搜索会跳过它。所做的工作是在根字典中将默认的样式添加到任何东西上。

然后参考下面的代码。

代码语言:javascript
运行
复制
<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>
代码语言:javascript
运行
复制
<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>

代码语言:javascript
运行
复制
  <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>
代码语言:javascript
运行
复制
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();
    }
}
代码语言:javascript
运行
复制
<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>
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29064007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档