Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource from Resource dictionary not getting applied

Tags:

wpf

xaml

I have a resource dictionary called mainpageresources.xaml stored in folder Resources as follows:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DataTemplate x:Key="CommandsTemplate">
        <ItemsControl ItemsSource="{Binding Path=Commands}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Margin="2,6">
                        <Hyperlink Command="{Binding Path=Command}">
                            <TextBlock Text="{Binding Path=DisplayName}" />
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>
    </ResourceDictionary>

In my MainWindow.xaml file I am trying to use this resource for an Item Control as follows but it doesn't seem to work. If I remove the comments from the below ItemsControl that works fine.

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Demo" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/MainWindowResources.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <ItemsControl ItemTemplate="{StaticResource ResourceKey=CommandsTemplate}">            
        </ItemsControl>

        <!--<ItemsControl ItemsSource="{Binding Path=Commands}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Margin="2,6">
                        <Hyperlink Command="{Binding Path=Command}">
                            <TextBlock Text="{Binding Path=DisplayName}" />
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>-->
    </Grid>
</Window>

Is there anything wrong here ?

like image 625
Frank Q. Avatar asked Mar 24 '26 00:03

Frank Q.


2 Answers

ItemTemplate is there for you to indicate what the template for each item should be. Instead, use a ContentPresenter.

<ContentPresenter Content="{Binding}" 
                  ContentTemplate="{StaticResource CommandsTemplate}" />
like image 69
Adam Robinson Avatar answered Mar 26 '26 06:03

Adam Robinson


That does not look right at all, i.e. the resource expanded:

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=Commands}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="2,6">
                            <Hyperlink Command="{Binding Path=Command}">
                                <TextBlock Text="{Binding Path=DisplayName}" />
                            </Hyperlink>
                        </TextBlock>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    <ItemsControl.ItemTemplate>
</ItemsControl>

Probably not what you want, no ItemsSource set, hence no items generated, and the ItemTemplate is what presumably should be your whole control. To just reference something use a ContentPresenter.

like image 26
H.B. Avatar answered Mar 26 '26 06:03

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!