Home WPF ItemsControl Template doesn't show ItemsControl.Items
Reply: 0

WPF ItemsControl Template doesn't show ItemsControl.Items

user3064
1#
user3064 Published in April 25, 2018, 6:08 pm

I want to create a Template or a Style from this xaml code without to define the ItemsControl.Items, the controls should to be exchangeable:

<ItemsControl Style="{DynamicResource ItemsControlWithMargin1}" Height="50">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="Control">
            <Setter Property="Margin" Value="15 0 0 0"></Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.Items>

        <Image Style="{DynamicResource WarningImage}"/>
        <Label Style="{DynamicResource InfoLabel1}">

    </ItemsControl.Items>

</ItemsControl>

I've tried it with a Style:

<Style x:Key="ItemsControlWithMargin1" TargetType="ItemsControl">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ItemsControl">

                            <ItemsControl>
                                <ItemsControl.ItemContainerStyle>
                                    <Style TargetType="Control">
                                        <Setter Property="Margin" Value="15 0 0 0"></Setter>
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

but when I set the Style I don't see my ItemsControl.Items in the orginal ItemsControl where my controls are in,... it's just blank. The same happens when i use this ControlTemplate:

<ControlTemplate x:Key="ItemsControlWithMargin2" TargetType="{x:Type ItemsControl}">

        <ItemsControl>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="Control">
                    <Setter Property="Margin" Value="15 0 0 0"></Setter>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

</ControlTemplate>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311289 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO