2016-11-14 93 views
0

第一關都:對不起,我的英文不好,我通常會說德語。無法編輯列表框,前景

目前我工作的一個大項目,我必須填寫列表框,這是我作爲「導航控制」用,用從字典字符串。

在這個項目的舊版本中,我手動將字符串放在列表框中,然後我可以將列表框的前景從黑色設置爲白色,但在當前版本中,它不再可能。

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

任何人都可以向我解釋爲什麼這不起作用?

+2

什麼被指定在sytle'MetroListBox'中?它可能會改寫 – Hank

+1

@Hank,本地值具有[優先](https://msdn.microsoft.com/en-us/library/ms743230(V = vs.110)的.aspx)在風格設置器,但也許是風格重新定義'ItemTemplate'('ListBoxItem')和/或面板樣式,因此設置'Foreground'對任何東西都沒有影響。 – Sinatr

回答

0

這是很難不喜歡什麼是對的ListBox項目模板一些更多的信息猜測?如果項目Foreground設置爲

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

在另一方面: 例如,你可以有下面的模板中的資源:

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

在這種情況下設置Foreground到某一值時會影響的項目,如datatemplate中的某個固定值,然後在ListBox中設置它也不起作用。