为什么IntelliSense没有使用DataGrid内置列显示项目的属性?

时间:2017-10-12 13:22:59

标签: wpf xaml intellisense wpfdatagrid design-time

我有这个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

的属性

enter image description here

因此,我创建了ItemsControl绑定到Items的{​​{1}}列表。当我编写单个显示项的Container的绑定时,IntelliSense现在向我显示DataTemplate类的属性:

enter image description here

如果我使用ItemListBox,也会发生同样的情况。

但是ListView出现了问题。如果我创建了一个内置模板的列,例如DataGrid,则智能感知会显示DataGridTextColumn的属性,而不是Container的属性!

enter image description here

其他内置列相同:Item等。

但是,如果我使用DataGridCheckBoxColumn编写自定义模板,则效果与DataGridTemplateColumn一样。

为什么会这样?如何使用ItemsControl的内置列(如果可能)使IntelliSense正常工作?

1 个答案:

答案 0 :(得分:1)

  

为什么会这样?

可能是因为DataGridColumn不是FrameworkElement被添加到可视树中。它没有DataContextDataTemplate的根元素确实会添加到可视化树中,但却有DataContext。这就是区别。

  

你的回答似乎很合理,但我仍然觉得很奇怪。对于像DataTemplate这样的内置列,我希望隐式DataGridCheckBoxColumn。如何将其呈现为CheckBox否则?

CheckBox最终是在运行时构建的,而不是在设计时构建的。设计人员不会运行在运行时执行的所有代码。