不能编辑listbox -前景

本文关键字:前景 listbox 编辑 不能 | 更新日期: 2023-09-27 18:17:54

首先:抱歉我的英语不好,我通常说德语。

目前我正在做一个大项目,我必须填充一个列表框,我用它作为"导航控制",从字典的字符串。

在这个项目的旧版本中,我手动将字符串放在列表框中,然后我可以将列表框的前景从黑色设置为白色,但在当前版本中,这是不可能的了。

   <StackPanel Grid.Column="0" Orientation="Vertical">
        <TextBlock Text=" Navigation" FontSize="16" Margin="10" Foreground="White"/>
        <ListBox HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            HorizontalContentAlignment="Left"
            Margin="10"
            MaxHeight="150"
            BorderBrush="Black"
            Style="{DynamicResource MetroListBox}"
            SelectedItem="{Binding SelectedTemplate}" Background="#002B2B2B" 
            ItemsSource="{x:Static models:TemplateRepository.TemplateDictionary}" 
            Foreground="White"
            DisplayMemberPath="Value">
        </ListBox>
    </StackPanel>

谁能给我解释一下为什么这不起作用?

不能编辑listbox -前景

如果没有更多的信息,比如ListBox的项目模板是什么,很难猜测?例如,您可以在参考资料中使用以下模板:

<DataTemplate  DataType="{x:Type local:SomeClass}">
    <Grid DataContext="{Binding}">
        <TextBlock Text="{Binding Path=Name}" Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ListBox}}" />
    </Grid>
</DataTemplate>

在这种情况下,将Foreground设置为某些值会影响项目,例如:

<ListBox x:Name="listBox" ItemsSource="{Binding}"  Foreground="Red">

另一方面,如果Items的Foreground在数据模板中被设置为某个固定值,那么在ListBox中也设置它将没有效果。