为什么这个数据模板在设计时不起作用?
以下简单数据模板仅在运行时有效。在设计时它什么也不显示。为什么会这样呢?
<DataTemplate x:Key="SomeEnumDataTemplate">
<ListBox Name="list" Width="20" IsSynchronizedWithCurrentItem="True" SelectedIndex="{Binding Mode=OneWay, Converter={StaticResource EnumToIntConverter}}">
<ListBox.Template>
<ControlTemplate TargetType="ListBox">
<ContentPresenter Content="{TemplateBinding SelectedItem}" />
</ControlTemplate>
</ListBox.Template>
<Rectangle Height="10" Width="10" Fill="Red" />
<Rectangle Height="10" Width="10" Fill="Green" />
<Rectangle Height="10" Width="10" Fill="Yellow" />
</ListBox>
</DataTemplate>
我在另一个 DataTemplate 中像这样使用它:
<HierarchicalDataTemplate x:Key="NodeDataTemplate" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" ToolTip="{Binding Description}">
<ContentControl ContentTemplate="{StaticResource SomeEnumDataTemplate}" Content="{Binding Mode}" Margin="3,0,0,0" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
它再次在具有设计时数据的 UserControl 中使用:
<UserControl x:Class="MyProject.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ViewModels="clr-namespace:MyProject.ViewModels" mc:Ignorable="d"
d:DesignHeight="780" d:DesignWidth="400" d:DataContext="{x:Static ViewModels:SampleData.RootNode}">
<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource NodeDataTemplate}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</UserControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以轻松创建设计时数据:
创建您的数据模型:
创建一个具有
.xaml
扩展名的文件,其中包含:DesignTimeTreeData.xaml
d:DataContext
和d:DesignData
来使用您在DesignTimeTreeData.xaml中指定的数据:MainWindow .xaml
Window.DataContext
属性通常设置为ViewModel
并且TreeView.DataContext
是它的集合,为了保持两个数据源都正常工作,您可以用Grid
包围TreeView
,其中DataContext
设置为ViewModel
> 的收藏。DummyViewModel.cs
MainWindow.xaml
结果:
编辑
下一个问题是:如何在设计器中扩展项目?
Person
将拥有一个人的集合:设计时数据将有一个子级
DesignTimeTreeData.xaml
树现在将有一个 HierarchicalDataTemplate:
<前><代码><分层数据模板
DataType =“{x:本地类型:人员}”
ItemsSource="{绑定子项}">
<标签内容=“{绑定名称}”/>
<标签内容=“{绑定年龄}”边距=“3,0,0,0”/>
并且
TreeView
将绑定到DesignerProperties.IsInDesignMode
以展开中的项目设计师:这是窗口的 xaml:
MainWindow.xaml
这是结果:
You can easily create design time data:
Create your DataModel:
Create a file with
.xaml
extension containing:DesignTimeTreeData.xaml
d:DataContext
andd:DesignData
to use the data you specified in the DesignTimeTreeData.xaml:MainWindow.xaml
Window.DataContext
property is set to aViewModel
and theTreeView.DataContext
is a collection of it, in order yo keep both datasources working, you can surround theTreeView
with aGrid
whichDataContext
is set to theViewModel
's collection.DummyViewModel.cs
MainWindow.xaml
Result:
Edit
The next question is going to be: Ho do I expand the items in the Designer?
The
Person
is going to have a collection of persons:The design time data is going to have a child
DesignTimeTreeData.xaml
The tree is going to have a HierarchicalDataTemplate now:
And the
TreeView
will bind to theDesignerProperties.IsInDesignMode
to expand the items in the designer:This is the xaml for the window:
MainWindow.xaml
And this is the result:
一旦你开始 Binding,设置 DataContext、ItemSource 等,设计器就会对你大发雷霆。只需删除所有绑定(看起来像 3 个),您的设计器就可以工作了。将其全部排列好,或者执行任何您需要执行的操作,然后重新添加绑定。
Once you start Binding, setting DataContext, ItemSource etc, the designer will crap out on you. just remove all your bindings (looks like 3) and your designer will work. Get it all lined up, or whatever you need to do, then add bindings back.