Cannot find a resource named "X", why not?

I want to create a user interface for the inventory management wizard. Corresponding line in xaml:

<ContentPresenter Content="{Binding Current}" ContentTemplateSelector="{StaticResource inventorySelector}"/>

Current is the currently active view model, one of the available: InventoriesViewModel, GroupViewModel, NewArticlesViewModel, ResultViewModel. DataTemplateSelector I am defined as such:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Centron.WPF.WarehousingExtension.InventoryModule.ViewModels.WizardViewModels;

namespace Centron.WPF.WarehousingExtension.InventoryModule.UI.DataTemplateSelectors
{
    public class InventoryDatatemplateSelector : DataTemplateSelector
    {
        public DataTemplate AvailableDatatype { get; set; }
        public DataTemplate GroupsDatatype { get; set; }
        public DataTemplate NewDatatype { get; set; }
        public DataTemplate ResultDatatype { get; set; }

        public InventoryDatatemplateSelector()
        {
            Debug.WriteLine("");
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is AvailableInventoriesViewModel)
                return AvailableDatatype;
            else if (item is GroupsViewModel)
                return GroupsDatatype;
            else if (item is NewArticlesViewModel)
                return NewDatatype;
            else return ResultDatatype;
        }
    }
}

Then I instantiate the DataTemplates and selector as follows:

<base:InventoryViewBase.Resources>
    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype">
        <controls:AvailableInventoriesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:GroupsViewModel" x:Key="groupsDatatype">
        <controls:GroupsView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:NewArticlesViewModel" x:Key="newArticlesDatatype">
        <controls:NewArticlesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:ResultViewModel" x:Key="resultDatatype">
        <controls:ResultView/>
    </DataTemplate>
    <selector:InventoryDatatemplateSelector
            x:Key="inventorySelector" 
            AvailableDatatype="{StaticResource availableInventoriesDatatype}"
            GroupsDatatype="{StaticResource groupsDatatype}"
            NewDatatype="{StaticResource newArticlesDatatype}"
            ResultDatatype="{StaticResource resultDatatype}"/>
</base:InventoryViewBase.Resources>

I set a breakpoint in the constructor of my InventoryDatatemplateSelector and can go through it, but in the next debugging step, apparently when it tries to set the first property of this selector instance, I immediately get an exception from the internal exception:

\ "availableInventoriesDatatype \". .

, , ?

+5
4

, . , "Key" . :

    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype" >
        <controls:AvailableInventoriesView />
    </DataTemplate>

:

    <DataTemplate x:Key="availableInventoriesDatatype" DataType="viewModels:AvailableInventoriesViewModel" >
        <controls:AvailableInventoriesView />
    </DataTemplate>
+17

, , , , . DataTemplates , DataTemplateSelector . DataTemplates , :

<DataTemplate DataType="{x:Type viewModels:AvailableInventoriesViewModel}">
    <controls:AvailableInventoriesView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:GroupsViewModel}">
    <controls:GroupsView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:NewArticlesViewModel}">
    <controls:NewArticlesView />
</DataTemplate>
<DataTemplate DataType="viewModels:ResultViewModel">
    <controls:ResultView/>
</DataTemplate>

ContentPresenter ContentTemplateSelector, :

<ContentPresenter Content="{Binding Current}" />

DataTemplate Current.

.

WPF , . , , , .

, .

+7

- ItemTemplate DataTemplate:

<ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
     ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
     ItemTemplate="{StaticResource nameItemTemplate}">
    </ListBox>

<Grid.Resources>
            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>          
</Grid.Resources>

Grid.Resources Listbox, , !

<Grid.Resources>
        <!-- Name item template -->
        <DataTemplate x:Key="nameItemTemplate">
            <Label Content="{Binding XPath=@Name}"/>
        </DataTemplate>          
    </Grid.Resources>

<ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
     ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
     ItemTemplate="{StaticResource nameItemTemplate}">
    </ListBox>

XAML ?:):)

0

I give more detailed information in the case of styling nested components. In this example, MainWindowGrid contains a UserControlGrid and a PanelGrid.

The following code will not work.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:XeerSoft.UX.Themes">
    <Thickness x:Key="PanelRowMargin">0,0,0,10</Thickness>
    <Thickness x:Key="PanelPadding">10</Thickness>

    <Color x:Key="Primary">#005C9D</Color>
    <SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource Primary}"></SolidColorBrush>

    <Color x:Key="Accent">#E3672B</Color>
    <SolidColorBrush x:Key="AccentBrush" Color="{StaticResource Accent}"></SolidColorBrush>

    <Color x:Key="DarkGray">#777777</Color>
    <SolidColorBrush x:Key="DarkGrayBrush" Color="{StaticResource DarkGray}"></SolidColorBrush>

    <Color x:Key="LightGray">#E4E4E4</Color>
    <SolidColorBrush x:Key="LightGrayBrush" Color="{StaticResource LightGray}"></SolidColorBrush>

    <Style x:Key="MainWindowGrid"
           BasedOn="{StaticResource {x:Type Grid}}"
           TargetType="Grid">
        <Setter Property="Background" Value="{StaticResource LightGrayBrush}"/>
    </Style>

    <Style x:Key="GridPanel"
           BasedOn="{StaticResource {x:Type Grid}}"
           TargetType="Grid">
        <Setter Property="Background" Value="White"/>
    </Style>

    <Style x:Key="UserControlPanel"
           BasedOn="{StaticResource {x:Type UserControl}}"
           TargetType="UserControl">
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}"/>
        <Setter Property="BorderThickness" Value="1"/>
    </Style>

</ResourceDictionary>

Now, if you replace the ad sequence so that MainWindowGrid continues, it will work. (External scope, last)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:XeerSoft.UX.Themes">
    <Thickness x:Key="PanelRowMargin">0,0,0,10</Thickness>
    <Thickness x:Key="PanelPadding">10</Thickness>

    <Color x:Key="Primary">#005C9D</Color>
    <SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource Primary}"></SolidColorBrush>

    <Color x:Key="Accent">#E3672B</Color>
    <SolidColorBrush x:Key="AccentBrush" Color="{StaticResource Accent}"></SolidColorBrush>

    <Color x:Key="DarkGray">#777777</Color>
    <SolidColorBrush x:Key="DarkGrayBrush" Color="{StaticResource DarkGray}"></SolidColorBrush>

    <Color x:Key="LightGray">#E4E4E4</Color>
    <SolidColorBrush x:Key="LightGrayBrush" Color="{StaticResource LightGray}"></SolidColorBrush>


    <Style x:Key="GridPanel"
           BasedOn="{StaticResource {x:Type Grid}}"
           TargetType="Grid">
        <Setter Property="Background" Value="White"/>
    </Style>

    <Style x:Key="UserControlPanel"
           BasedOn="{StaticResource {x:Type UserControl}}"
           TargetType="UserControl">
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}"/>
        <Setter Property="BorderThickness" Value="1"/>
    </Style>

    <Style x:Key="MainWindowGrid"
           BasedOn="{StaticResource {x:Type Grid}}"
           TargetType="Grid">
        <Setter Property="Background" Value="{StaticResource LightGrayBrush}"/>
    </Style>
</ResourceDictionary>
0
source

All Articles