为什么 IntelliSense 不显示带有 DataGrid 内置列的项目属性?
Why IntelliSense doesn't show the item's properties with DataGrid built-in columns?
我有这个 ViewModel classes:
public class Item
{
public string ItemName { get; set; }
}
public class Container
{
public string ContainerName { get; set; }
public List<Item> Items { get; }
}
我在 Window 的 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
绑定到 Container
的 Items
列表。当我编写单个显示项的 DataTemplate
的绑定时,IntelliSense 现在会向我显示 Item
class:
的属性
如果我使用 ListBox
或 ListView
,也会发生同样的情况。
但是 DataGrid
出问题了。如果我使用内置模板创建列,例如 DataGridTextColumn
,IntelliSense 会向我显示 Container
的属性,而不是 Item
的属性!
其他内置列也一样:DataGridCheckBoxColumn
,等等
但是,如果我使用 DataGridTemplateColumn
编写自定义模板,它会像 ItemsControl
一样工作得很好。
为什么会这样?我怎样才能使 IntelliSense 与 DataGrid
的内置列一起正常工作(如果可能的话)?
Why is this happening?
可能是因为 DataGridColumn
不是添加到可视化树中的 FrameworkElement
。它没有 DataContext
。 DataTemplate
的根元素确实被添加到可视化树中并且有一个 DataContext
。这就是区别。
Your answer seems reasonable, but there's a thing that I still found weird. I would expect an implicit DataTemplate
also for built-in columns like DataGridCheckBoxColumn
. How could it be rendered as a CheckBox
otherwise?
CheckBox
最终在 运行 时构建,而不是在设计时构建。设计者没有 运行 在 运行 时间执行的所有代码。
我有这个 ViewModel classes:
public class Item
{
public string ItemName { get; set; }
}
public class Container
{
public string ContainerName { get; set; }
public List<Item> Items { get; }
}
我在 Window 的 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
绑定到 Container
的 Items
列表。当我编写单个显示项的 DataTemplate
的绑定时,IntelliSense 现在会向我显示 Item
class:
如果我使用 ListBox
或 ListView
,也会发生同样的情况。
但是 DataGrid
出问题了。如果我使用内置模板创建列,例如 DataGridTextColumn
,IntelliSense 会向我显示 Container
的属性,而不是 Item
的属性!
其他内置列也一样:DataGridCheckBoxColumn
,等等
但是,如果我使用 DataGridTemplateColumn
编写自定义模板,它会像 ItemsControl
一样工作得很好。
为什么会这样?我怎样才能使 IntelliSense 与 DataGrid
的内置列一起正常工作(如果可能的话)?
Why is this happening?
可能是因为 DataGridColumn
不是添加到可视化树中的 FrameworkElement
。它没有 DataContext
。 DataTemplate
的根元素确实被添加到可视化树中并且有一个 DataContext
。这就是区别。
Your answer seems reasonable, but there's a thing that I still found weird. I would expect an implicit
DataTemplate
also for built-in columns likeDataGridCheckBoxColumn
. How could it be rendered as aCheckBox
otherwise?
CheckBox
最终在 运行 时构建,而不是在设计时构建。设计者没有 运行 在 运行 时间执行的所有代码。