2008-10-30 71 views
18

Tôi đã tạo ra những phong cách sau đây cho một ListBox mà sẽ có một hình ảnh hiển thị bên cạnh một số văn bản:Thay đổi màu Foreground của một ContentPresenter trong một ListBox

<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter --> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="SnapsToDevicePixels" Value="true"/> 
       <Setter Property="OverridesDefaultStyle" Value="true"/> 
       <Setter Property="VerticalContentAlignment" Value="Center"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
          <Grid SnapsToDevicePixels="true"> 
           <Border x:Name="Border"> 
            <Grid Height="40"> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="Auto"/> 
              <ColumnDefinition Width="*"/> 
             </Grid.ColumnDefinitions> 
             <Image 
              x:Name="DisplayImage" 
              Source="{Binding Path=ThumbnailImage}" 
              Height="30" 
              Width="30" 
              Grid.Column="0"/> 

             <ContentPresenter 
              x:Name="DisplayText" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Center" 
              Grid.Column="1"/> 
             <!--<ContentPresenter.Resources> 
               <Style TargetType="{x:Type TextBlock}"> 
                <Setter Property="Foreground" Value="Black"/> 
               </Style> 
              </ContentPresenter.Resources>--> 

             <!--Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath, Converter={StaticResource myDisplayMemberConverter}}"--> 
             <!--<Label 
              x:Name="Text" 
              Content="{Binding Path=FullNameAndTitle}" 
              Foreground="Black" 
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
              VerticalContentAlignment="Center" 
              HorizontalAlignment="Stretch" 
              Grid.Column="1" 
              Height="40"/>--> 
            </Grid> 
           </Border> 
          </Grid> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsSelected" Value="true"> 
            <!--<Setter Property="FontWeight" Value="Bold" TargetName="DisplayText"/>--> 
            <!--<Setter Property="Style" Value="{StaticResource SelectedTextStyle}" TargetName="DisplayText"/>--> 
            <Setter Property="Background" Value="DarkBlue" TargetName="Border"/> 
            <Setter Property="Width" Value="40" TargetName="DisplayImage"/> 
            <Setter Property="Height" Value="40" TargetName="DisplayImage"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBox}"> 
       <Grid> 
        <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}"> 
         <Grid> 
          <ScrollViewer Margin="1,1,1,1" Focusable="false" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
           <StackPanel IsItemsHost="true"/> 
          </ScrollViewer> 
         </Grid> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsGrouping" Value="true"> 
         <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

tôi phải sử dụng ContentPresenter như tôi lọc những gì được hiển thị (văn bản khôn ngoan) bằng cách sử dụng DisplayMemberPath của ListBox chính nó.

Tất cả những gì tôi muốn làm là đặt FontWeight thành In đậm và Mặt trước thành Trắng khi một mục được chọn trong ListBox.

Có ai gặp phải sự cố như thế này không? Tôi đã xem xét một số câu hỏi liên quan nhưng mọi người đã có thể sử dụng TextBlock để giải quyết các vấn đề của họ.

Mọi thông tin ppl có thể cung cấp sẽ được đánh giá cao.

Cheers

+0

Bạn cũng có thể thiết lập các 'TextElement.Foreground' đính kèm tài sản trực tiếp trên một điều khiển cha mẹ. – Sheridan

Trả lời

37

Ngoài ra còn có một cách khác. Bạn có thể thêm vào số ContentPresenter thuộc tính này

TextBlock.Foreground="YourColour" 

Trong trường hợp này, bạn cũng có thể sử dụng hoạt ảnh trên thuộc tính đó.

+5

Điều này dường như không hoạt động với các ràng buộc. –

+1

Điều này không hoạt động ở cuối của tôi – Akanksha

18

Đó là tất cả ok, tôi đã cố gắng trả lời câu hỏi này bản thân mình, tôi đã cố gắng để thay đổi foreground/FontWeight của ContentPresenter mà không chứa một định nghĩa cho foreground/FontWeight tất cả tôi chỉ đơn giản là cần thiết để làm được điều này:

<Setter Property="FontWeight" Value="Bold"/> 
<Setter Property="Foreground" Value="White"/> 

tức tháo:

TargetName="DisplayText" 
8

Dựa trên this related answer, tôi đã có thể để giải quyết một vấn đề tương tự với những điều sau:

<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" /> 
1
<Storyboard x:Key="Storyboard1"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="myContentPresenter"> 
     <EasingColorKeyFrame KeyTime="0" Value="Black"/> 
     <EasingColorKeyFrame KeyTime="0:0:0.2" Value="White"/> 
     </ColorAnimationUsingKeyFrames>   
    </Storyboard> 
Các vấn đề liên quan