繁体   English   中英

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

[英]How do you use a DataTemplate defined in Application.Resources?

如果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,但是以某种方式显示的文本只是说出我的视图模型类的名称“ 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?

将您的数据模板添加到字典中。 必须具有应用程序资源应具有的默认样式。 请参阅链接以获取更多说明。 没有任何样式的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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM