Handsome Greg
Handsome Greg

Reputation: 325

Inherit foreground value from the parent just as TextBlock in UWP

When I set the SelectedForeground in my ListViewItem style, the TextBlock inside the item DataTemplate changes the colour on selection, as expected. I would like the same colour to be applied to other elements in the DataTemplate, such as Path or Rectangle:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock Text="Hello" />
            <Path Fill={?} />
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

I tried to create a custom control and apply the template:

<ControlTemplate x:Key="myControlTemplate" TargetType="local:myControl">
    <Grid>
        <TextBlock Text="Hello" />
        <Path Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</ControlTemplate>

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <controls:myControl Template="{StaticResource myControlTemplate}"/>            
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

In that case the path does inherit the value from the ListViewItemPresenter, but only once - it does not update the value when selection changes.

How can I force the Path.Fill property to update it's colour just as TextBlock does?

Upvotes: 1

Views: 2042

Answers (2)

user3777939
user3777939

Reputation: 401

You can use relativeSource directly in your listView's item template.

        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Rectangle Width="24"
                               Height="24"
                               Fill="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <TextBlock Text="{Binding}"
                               x:Name="TitleBlock"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>

Upvotes: 2

Inside a ControlTemplate, the "templated parent" is the control itself, so adding a custom control into the mix isn't going to help you any.

What you probably want is this. I can't test it in UWP here, so let me know if you get any errors.

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock Text="Hello" />
            <Path 
                x:Name="MyPath"
                Fill="YellowGreen"
                Data="M 0,0 L 16,0 L 16,16 L 0,16 Z"
                />
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger 
                Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem}}" 
                Value="True">
                <Setter 
                    TargetName="MyPath"
                    Property="Fill"
                    Value="{Binding SelectedForeground, RelativeSource={RelativeSource AncestorType=ListView}}" 
                    />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ListView.ItemTemplate>

Upvotes: 0

Related Questions