我有这个ViewModel类:
public class Item
{
public string ItemName { get; set; }
}
public class Container
{
public string ContainerName { get; set; }
public List<Item> Items { get; }
}
我在我的窗口的XAML中使用它们来帮助设计时的IntelliSense:
<Window x:Class="DesignTime.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DesignTime"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:Container, IsDesignTimeCreatable=True}"/>
现在,IntelliSense可识别Container
:
因此,我创建了ItemsControl
绑定到Items
的{{1}}列表。当我编写单个显示项的Container
的绑定时,IntelliSense现在向我显示DataTemplate
类的属性:
如果我使用Item
或ListBox
,也会发生同样的情况。
但是ListView
出现了问题。如果我创建了一个内置模板的列,例如DataGrid
,则智能感知会显示DataGridTextColumn
的属性,而不是Container
的属性!
其他内置列相同:Item
等。
但是,如果我使用DataGridCheckBoxColumn
编写自定义模板,则效果与DataGridTemplateColumn
一样。
为什么会这样?如何使用ItemsControl
的内置列(如果可能)使IntelliSense正常工作?
答案 0 :(得分:1)
为什么会这样?
可能是因为DataGridColumn
不是FrameworkElement
被添加到可视树中。它没有DataContext
。 DataTemplate
的根元素确实会添加到可视化树中,但却有DataContext
。这就是区别。
你的回答似乎很合理,但我仍然觉得很奇怪。对于像
DataTemplate
这样的内置列,我希望隐式DataGridCheckBoxColumn
。如何将其呈现为CheckBox
否则?
CheckBox
最终是在运行时构建的,而不是在设计时构建的。设计人员不会运行在运行时执行的所有代码。