none
Foreground doesn't propagate to Label in DataTemplate RRS feed

  • Question

  • Say, I have following control and data templates.

    ListBox template:

    <Style TargetType="{x:Type ListBox}">
    	<Setter Property="Template">
    		<Setter.Value>
    			<ControlTemplate TargetType="{x:Type ListBox}">
    				<Border Name="Border" Background="{StaticResource ListBoxBackgroundBrush}" BorderBrush="{StaticResource StandardBorderBrush}" BorderThickness="1" CornerRadius="3">
    					<ScrollViewer Focusable="False">
    						<ItemsPresenter Margin="2"/>
    					</ScrollViewer>
    				</Border>
    			</ControlTemplate>
    		</Setter.Value>
    	</Setter>
    </Style>

    ListBoxItem template:

    <Style TargetType="{x:Type ListBoxItem}">
    	<Setter Property="Template">
    		<Setter.Value>
    			<ControlTemplate TargetType="{x:Type ListBoxItem}">
    				<Border Name="Border" BorderThickness="2" CornerRadius="3" Padding="1" SnapsToDevicePixels="True">
    					<ContentPresenter x:Name="Presenter"/>
    				</Border>
    				<ControlTemplate.Triggers>
    					<Trigger Property="IsSelected" Value="True">
    						<Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
    						<Setter TargetName="Border" Property="TextBlock.Foreground" Value="{StaticResource SelectedForegroundBrush}"/>
    						<Setter TargetName="Presenter" Property="ContentControl.Foreground" Value="{StaticResource SelectedForegroundBrush}"/>
    					</Trigger>
    				</ControlTemplate.Triggers>
    			</ControlTemplate>
    		</Setter.Value>
    	</Setter>
    </Style>

    DataTemplate:

    <DataTemplate x:Key="ActEntryDataTemplate">
    	<StackPanel Orientation="Horizontal">
    		<Label Content="From Data Template:"/>
    		<TextBlock Text="{Binding}" VerticalAlignment="Center"/>
    	</StackPanel>
    </DataTemplate>

    Applying data template:

    var listBox = new ListBox();
    listBox.ItemTemplate = FindResource("ActEntryDataTemplate") as DataTemplate;
    listBox.Items.Add("Item 1");

    However, when I select an item, the Label of DataTemplate doesn't receive white color, while TextBlock does:



    In 3-rd Setter I tried to set Label.Foreground instead of ContentControl.Foreground, but it didn't work either.


    There is no knowledge that is not power.


    • Edited by JohnyL Thursday, August 15, 2019 5:37 PM
    Thursday, August 15, 2019 5:36 PM

Answers

  • Hi  JohnyL,     

    Please use the TextBlock to avoid this issue(currently, no reasonable explanation was found by searching)

       <DataTemplate x:Key="ActEntryDataTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="From Data Template:" />
                    <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                </StackPanel>
            </DataTemplate>
    

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JohnyL Friday, August 16, 2019 4:10 PM
    Friday, August 16, 2019 6:06 AM
    Moderator

All replies

  • Hi  JohnyL,     

    Please use the TextBlock to avoid this issue(currently, no reasonable explanation was found by searching)

       <DataTemplate x:Key="ActEntryDataTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="From Data Template:" />
                    <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                </StackPanel>
            </DataTemplate>
    

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JohnyL Friday, August 16, 2019 4:10 PM
    Friday, August 16, 2019 6:06 AM
    Moderator
  • @Yong Lu Thanks! Anyway, it's really weird the propagation doesn't work.

    There is no knowledge that is not power.

    Friday, August 16, 2019 4:10 PM