2

次のように、mainpageresources.xamlというリソースディクショナリがResourcesフォルダーに格納されています。

<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>

MainWindow.xamlファイルで、このリソースをアイテムコントロールに次のように使用しようとしていますが、機能していないようです。以下のItemsControlからコメントを削除すると、正常に機能します。

<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>

ここに何か問題がありますか?

4

2 に答える 2

2

ItemTemplate各アイテムのテンプレートがどうあるべきかを示すためにあります。代わりに、を使用してContentPresenterください。

<ContentPresenter Content="{Binding}" 
                  ContentTemplate="{StaticResource CommandsTemplate}" />
于 2012-08-24T02:49:10.700 に答える
1

それはまったく正しく見えません。つまり、リソースが拡張されています。

<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>

おそらくあなたが望むものではなく、ItemsSourceセットも、したがってアイテムも生成されません、そしてそれItemTemplateはおそらくあなたの全体のコントロールであるべきです。何かを参照するには、を使用しContentPresenterます。

于 2012-08-24T02:50:34.590 に答える