gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:23:21 25 4
gpt4 key购买 nike

如果我的窗口无法访问其中定义的资源,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();
}
}

主窗口.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 不起作用。当我运行它时,我得到一个窗口,根本没有文本框,但是以某种方式显示的文本只是说我的 View 模型类的名称“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>

应用程序.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?

最佳答案

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

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible.

One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

然后引用下面的代码。

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

关于c# - 如何使用 Application.Resources 中定义的 DataTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com