I have this ViewModel classes:
public class Item
{
public string ItemName { get; set; }
}
public class Container
{
public string ContainerName { get; set; }
public List<Item> Items { get; }
}
I use them in the XAML of my Window to help the IntelliSense at DesignTime:
<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}"/>
Now the IntelliSense recognizes the properties of the Container
:
So I create an ItemsControl
bound to the Items
list of the Container
. When I write the bindings of the DataTemplate
of the single displayed item, the IntelliSense now shows me the properties of the Item
class:
The same happens if I use a ListBox
or a ListView
.
But with a DataGrid
something goes wrong. If I create a column with a built-in template, like DataGridTextColumn
, the IntelliSense shows me the Container
's properties, not the Item
's one!
Same for the other built-in columns: DataGridCheckBoxColumn
, etc.
However, if I write a custom template using the DataGridTemplateColumn
, it works well like the ItemsControl
.
Why is this happening? How can I make the IntelliSense to work correctly with the DataGrid
's built-in columns (if it's possible)?
Why is this happening?
Probably because a DataGridColumn
is not a FrameworkElement
that gets added to the visual tree. It doesn't have a DataContext
. The root element of a DataTemplate
does get added to the visual tree and has a DataContext
though. That's the difference.
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?
The CheckBox
is built eventually at runtime, not at design time. The designer doesn't run all code that is executed at runtime.