ItemTemplate from window resource not found

I have a window that shows 2 lists that should use the same template, so I thought about placing the template in a resource in my window as follows:

<Grid>
    <Grid.Resources>
        <DataTemplate DataType="itemTemplateAsResourceTest:FeatureViewModel" x:Key="FeatureTemplate">
            <TextBlock Margin="5" Text="{Binding Name}"/>
        </DataTemplate>
    </Grid.Resources>

    <ListBox ItemTemplate="{StaticResource FeatureTemplate}" ItemsSource="{Binding Features}"/>
</Grid>

Featuresis just a list FeatureViewModel(I put it in my Mainwindow for a test application), and FeatureViewModel just looks in this test:

public class FeatureViewModel
{
    public string Name
    {
        get { return "Name"; }
    }
}

It works in Designer, however, when I launch my application, it says that the "Resource" FeatureTemplate "was not found." It cannot be mistreated because the designer finds it! What did I do wrong?

When I put the template directly in the ListBox, it works, but then I duplicate my template:

<Grid>
    <ListBox ItemsSource="{Binding Features}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="itemTemplateAsResourceTest:FeatureViewModel">
                <TextBlock Margin="5" Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Window , - ?

<DataTemplate DataType="itemTemplateAsResourceTest:FeatureViewModel" x:Key="FeatureTemplate">

<DataTemplate DataType="{x:Type itemTemplateAsResourceTest:FeatureViewModel}" x:Key="FeatureTemplate">

!

+4
1

DataType. , x:Key.

, , ( , , Style.TargetType) {x:Type ...}. DataType ( object) , XAML, Type, .

+1

Source: https://habr.com/ru/post/1626529/


All Articles