Привязка к шаблону "Передний план" не работает - PullRequest
4 голосов
/ 19 августа 2009

У меня есть список, шаблон элемента которого является списком. Я пытаюсь установить свойство "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 голосов
/ 24 августа 2012

Этот пример работает для меня - он может относиться к тому, что вы пытаетесь сделать:

    <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>
...