- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 ResourceDictionary
中有几个 DataTemplate:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:z="clr-namespace:ZoomPan">
<DataTemplate DataType="{x:Type z:Circle}">
<z:Circle Center="{Binding Center}" Radius="{Binding Radius}" x:Name="circle"/>
<DataTemplate.Triggers>
<DataTrigger ... />
</DataTemplate.Triggers>
</DataTemplate>
.... etc.
</ResourceDictionary>
我在窗口
中使用它:
<z:MyUserControl>
<z:MyUserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</z:MyUserControl.Resources>
</z:MyUserControl>
数据模板和触发器工作正常。
我有两个问题:
为什么 ResourceDictionary 中的 DataTemplate 不需要 x:Key?
我可以用 Key 以某种方式将 ResourceDictionary 划分为多个部分,然后指向一个部分中的 DataTemplates 吗?
最佳答案
Why do DataTemplates in a ResourceDictionary not need a x:Key?
当您缺少 Style
或 DataTemplate
的键时,此结构:
<DataTemplate TargetType="{x:Type local:MyType}">
自动转换为:
<DataTemplate x:Key="{x:Type local:MyType}" TargetType="{x:Type local:MyType}">
这意味着 Style/DataTemplate 将显式用于此类型的所有控件,因为在 ResourceDictionaries
中不能是没有键的元素,而是这样做是为了简化结构。引用自MSDN
:
<小时/>Setting the TargetType property to the TextBlock type without setting an x:Key
implicitly sets the x:Key to {x:Type TextBlock}.
This also means that if you give the above Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.
When I give the DataTemplates a Key they stop to work
当你为DataTemplate
指定一个key时,应该使用这个key,如:
<ContentControl Name="MyContent"
ContentTemplate="{StaticResource MainView}"> // here set the key
<MainWindowViewModels:MainViewModel />
</ContentControl>
<小时/>
Can I divide the ResourceDictionary somehow in sections with a Key and then point to the DataTemplates in a section?
不幸的是,ResourceDictionary 是 Quote
:
class is not derived from
DictionaryBase
. Instead, the ResourceDictionary class implementsIDictionary
but relies on a Hashtable internally.
在这种情况下,它不是有序字典,其中每个元素都分配给哈希。用于分离字典区域使用 ResourceDictionary.MergedDictionaries
.
关于wpf - WPF 中的 ResourceDictionary 中没有键的 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261756/
我正在使用 DataTemplate 将 View 应用于 ViewModel。我有一种情况,当 View 的一个实例(DataTemplate)中发生某件事时,我需要在所有其他实例中采取行动。 我已
如何从 DataTemplate 派生新类,然后用它代替 DataTemplate? 例如,在 C# 中: public class DerivedDataTemplate : DataTemplat
我有以下xaml; 当我使用 Node 模板显示 No
我想在 dataTemplate 中使用 dataTemplale。我想像这样在列表框中显示数据: 这就是我得到的。它不起作用。 cl
大家好,我正在尝试更改 dataGrid 中列的模板,但我找不到在 XAML 中执行此操作的方法。我正在尝试通过这种方式来做
wpf 应用程序中的默认 DataTemplate 显示 .ToString() 方法的结果。我正在开发一个默认 DataTemplate 不显示任何内容的应用程序。 我试过:
我发现自己有点束手无策……有束缚……呵呵……(跛脚) 无论如何...我需要引用主 ViewModel 的属性,但来自 DataTemplate ,它本身在另一个 DataTemplate 中... 看
我已经为我喜欢的项目中的一些数据类型创建了几个数据模板。这些数据模板真的很酷,因为它们像魔术一样工作,无论何时何地出现在 UI 中,都神奇地改变了数据类型实例的外观。现在我希望能够在一个特定的列表框中
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Datatemplate inheritance 我有几个不是子类的数据类型,它们也不共享一个接口(interfa
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我创建了一个 DataTemplateSelector,它使用一组已知接口(interface)进行初始化。如果传递到选择器的项目实现了这些接口(interface)之一,则返回关联的数据模板。 首先
是否可以将数据模板用于没有 ListBox 或其他项目控件的单个项目? 我有一个数据模板,我想在 xaml 中实例化,而不是在列表中,只是在边框内,并设置其数据上下文。 类似(伪):
在我的 MainWindow.xaml 中,我对 ResourceDictionary 有以下引用: 在 Main
我在数据模板中有 WPF ComboBox(列表框中有很多组合框),我想处理输入按钮。如果它是例如,那将很容易。一个按钮 - 我会使用命令 + 相对绑定(bind)路径等。不幸的是,我不知道如何使用命
我想对 ItemsCollection 进行数据绑定(bind),但不是渲染集合项,而是渲染通过集合项上的属性到达的子对象。 更具体地说:这将是游戏的 2D map 查看器(尽管在当前状态下它还不是
我使用 MasterDetailsView控制。它有 MasterHeaderTemplate属性(property)。我想添加一个 TextBox以便执行元素搜索。我不明白该怎么做。因为DataTe
我为自己设计的一个非常基本的 WPF 练习遇到了一个奇怪的问题,即从 ViewModel 动态填充菜单。给定以下主窗口标记:
在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 对控件进行数据绑定(bind)期间不会抛出异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 WIN7
我有一个关于 this one 的问题:我正在尝试将一个事件附加到我的 StackPanel,但在使用 XamlReader 时它似乎没有连接。我无法调用 ChildItem_Load 方法。有谁知道
我是一名优秀的程序员,十分优秀!