Style within DataTemplate is only being applied to the last item in ItemsControl?

前端 未结 2 1813
太阳男子
太阳男子 2020-12-10 14:03

In the XAML below, I have an ItemsControl that has three DataObjects.
I use a DataTemplate to display DataObjects as Buttons with an \"X\" on them.
The Button uses a

相关标签:
2条回答
  • 2020-12-10 14:43

    The answer to this is rather simple actually, every visual can only be the child of one object, unlike text like "X" which is just data.

    If you create a style like this:

    <Style>
        <Setter Property="Content">
            <Setter.Value>
                 <TextBlock Text="X"/>
            </Setter.Value>
        </Setter>
    <Style>
    

    Only one TextBlock is created for all instances on which the style is applied, so the TextBlock will "jump" on every application and end up at the last item.

    If you set the ContentTemplate however you create as the name implies a template which is used to generate the content independenctly for every object so you end up with one instance per control where the style applies.

    0 讨论(0)
  • 2020-12-10 14:45

    Here's a working sample:

    <Window x:Class="Styles.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Styles"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <Style x:Key="A" TargetType="{x:Type Button}">
                    <Style.Setters>
                        <Setter Property="Content" Value="X"></Setter>
                    </Style.Setters>
                </Style>
            </Grid.Resources>
            <ItemsControl>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Height="24" Width="24" Style="{StaticResource A}">
    
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.Items>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                </ItemsControl.Items>
            </ItemsControl>
        </Grid>
    
    </Window>
    

    Edit1 Doh.. Got it working, the trick is to use ContentTemplate.

    <Window x:Class="Styles.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Styles"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <DataTemplate x:Key="A">
                    <TextBlock>X</TextBlock>
                </DataTemplate>
            </Grid.Resources>
            <ItemsControl>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Height="24" Width="24" ContentTemplate="{StaticResource A}">
    
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.Items>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                </ItemsControl.Items>
            </ItemsControl>
        </Grid>
    
    </Window>
    

    Edit2: A sample of more complex ContentTemplate:

    <Window x:Class="Styles.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Styles"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <DataTemplate x:Key="A">
                    <StackPanel Width="30" Orientation="Horizontal">
                        <Grid Background="White" Width="10" Height="10"></Grid>
                        <Grid Background="Blue" Width="10" Height="10"></Grid>
                        <Grid Background="Red" Width="10" Height="10"></Grid>
                    </StackPanel>
                </DataTemplate>
            </Grid.Resources>
            <ItemsControl>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Height="24" Width="34" ContentTemplate="{StaticResource A}">
    
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.Items>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                    <DataObject></DataObject>
                </ItemsControl.Items>
            </ItemsControl>
        </Grid>
    
    </Window>
    
    0 讨论(0)
提交回复
热议问题