模板绑定“Foreground”无法工作

4

我有一个列表框,其中的项模板是一个列表框。我试图将内部列表框的 "Foreground" 属性设置为与主列表框相同。但这并没有成功。以下是代码段。在这里,"Foreground="{TemplateBinding Foreground}" 没有效果。

<ListBox x:Name="GroupListBox" Grid.Column="1" Grid.Row="1" Style="{StaticResource ListBoxStyle1}" Visibility="Collapsed"
             BorderBrush="Transparent" Background="Transparent" Foreground="{Binding WebForeground}">
        <ListBox.ItemTemplate>
            <DataTemplate x:Name="test">
                <StackPanel Orientation="Horizontal" >
                    <!--<TextBlock Text="{Binding Rank}" FontFamily="Arial" FontSize="13" TextDecorations="Underline" TextWrapping="Wrap" Width="115" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,15,0,0"/>-->
                    <ListBox x:Name="SubGroupListBox" ItemsSource="{Binding InnerList }" ItemTemplate="{StaticResource ItemTemplateKey1}" 
                             ItemsPanel="{StaticResource ItemsPanelKey}" Style="{StaticResource ListBoxStyle1}" 
                             BorderBrush="Transparent" Background="Transparent" Foreground="{TemplateBinding Foreground}">                            
                    </ListBox>
             </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
1个回答

2

这个例子对我有效 - 它可能适用于您所尝试的内容:

    <ListBox x:Name="GroupListBox" Foreground="Purple">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <ListBox Foreground="{Binding Foreground, RelativeSource={RelativeSource Self}}">
                    <TextBox Text="{Binding Mode=OneWay}" FontSize="35" Foreground="{Binding Foreground, RelativeSource={RelativeSource Self}}"  />
                </ListBox>
         </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.ItemsSource>
        <x:Array Type="{x:Type sys:String}">
            <sys:String>Sample Data</sys:String>
            </x:Array>
    </ListBox.ItemsSource>

</ListBox>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接