2013-02-04 61 views
0
<Window.Resources> 
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}"> 
     <Setter Property="Background" Value="{StaticResource ResourceKey=ListboxBack}"/> 
     <Setter Property="Foreground" Value="Green"/> 
     <Setter Property="Width" Value="284"/> 
     <Setter Property="Height" Value="332"/> 
     <Setter Property="Margin" Value="18,77,0,151"/> 
     <Setter Property="ItemTemplate" Value="{DynamicResource DataTemplate1}"/> 
     <Setter Property="HorizontalAlignment" Value="Left"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Padding" Value="0,0,0,0"/> 
</Style> 

    <DataTemplate x:Key="DataTemplate1"> 
     <Grid Width="276" Height="36" Background="{x:Null}" Opacity="1"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="0.069*"/> 
       <ColumnDefinition Width="0.931*"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock x:Name="recback" Padding="40,0,0,0" Text="{Binding [0], FallbackValue=Number}" Width="Auto" HorizontalAlignment="Stretch" Margin="-1.899,0,-5.334,0" Grid.Column="0" FontSize="13.333" Height="38.277" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" Opacity="1" Grid.ColumnSpan="2" /> 
      <Rectangle HorizontalAlignment="Stretch" Height="1" Margin="3.5,0" VerticalAlignment="Bottom" Width="Auto" Fill="White" Grid.ColumnSpan="2"/> 
     </Grid> 
    </DataTemplate> 
</Window.Resources> 

<ListBox Style="{StaticResource ResourceKey=ListBoxStyle}" BorderThickness="0" x:Name="listBox1" Foreground="White" FontSize="18" d:LayoutOverrides="VerticalAlignment" BorderBrush="{x:Null}" /> 

我使用DataTemplate創建了ListBoxDataTemplate包含RectangleTextblock。當我在ListBox中選擇項目時,我想要更改TextBlock前景和Rectangle背景。你可以幫幫我嗎?如何更改列表框選定的項目背景和前景?

回答

0

使用類似於以下的方法。通過這種方式,您將覆蓋僅使用此列表框的指定x:Key的默認畫筆。也許你需要額外或不同x:Key s到覆蓋

<ListBox> 
    <ListBox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" /> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Green" /> 
    </ListBox.Resources> 
</ListBox> 

通過再次閱讀你的問題我明白,或許你還需要DataTriggers在你的DataTemplate。 您也可以嘗試這樣的事情注意Forground和背景應該在風格不適合這個代碼設置的TextBlock工作:

<TextBlock x:Name="recback" Padding="40,0,0,0" Text="{Binding [0], FallbackValue=Number}" Width="Auto" 
      HorizontalAlignment="Stretch" Margin="-1.899,0,-5.334,0" Grid.Column="0" FontSize="13.333" Height="38.277" 
      VerticalAlignment="Top" Opacity="1" Grid.ColumnSpan="2"> 
     <TextBlock.Style> 
      <Style TargetType="{x:Type TextBlock}"> 
       <Setter Property="Foreground" Value="Black"/> 
       <Setter Property="Background" Value="{x:Null}"/> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True"> 
         <Setter Property="Foreground" Value="Red"/> 
         <Setter Property="Background" Value="Yellow"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 
+0

對不起,我不這樣你understant understant您answer.I希望我的問題 ? –

+0

我想我明白你的問題。只需將ListBox.Resources部分複製到您的列表框中,然後再次檢查是否適合您 – iltzortz

+0

woow謝謝您的回答,並且我還想更改前景。 –

相關問題