locked
Style Override RRS feed

  • Question

  • Hi,


    I have a style im my application for the listview item, this style also changes the foreground of the listviewitem when the item is selected to a blue like color.
    Now I am trying to use this style in a listview but I want the style to do all the other effects specified but not change the foreground of the listviewitem because
    the foreground of the listview item is different according to specific condition specified in a binding. Below is the code of the style

    <Style TargetType="{x:Type ListViewItem}" x:Key="ListViewStyle">
      <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      <Setter Property="Background" Value="{StaticResource DefaultControlBackgroundBrush}"/>
      <Setter Property="Foreground" Value="{StaticResource DefaultControlForegroundBrush}" />
      <Setter Property="BorderThickness" Value="0,1,0,0" />
      <Setter Property="Height" Value="24" />
      <Setter Property="Padding" Value="0" />
      <Setter Property="BorderBrush" Value="{StaticResource GridLineColorBrush}" />
      <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
          <Setter Property="Background" Value="{StaticResource AlternateRowBackgroundBrush}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True" >
           <Setter Property="Background" Value="{StaticResource RowHoverBackgroundBrush}" />
           <Setter Property="BorderBrush" Value="Transparent" />
        </Trigger>
        <Trigger Property="IsSelected" Value="True" >
          <Setter Property="Background" Value="{StaticResource HeaderHighlightedBackgoundBrush}" />
          <Setter Property="Foreground" Value="{StaticResource DefaultControlForegroundBrush}"/> <!-- This Bit Changes the foreground color -->
          <Setter Property="BorderBrush" Value="{StaticResource HeaderHighlightedBackgoundBrush}" />
        </Trigger>
      </Style.Triggers>
    </Style>

    Now my listview is this:

    <ListView Grid.Row="1" Margin="5" Name="listView1" ItemsSource="{Binding}"   PreviewMouseDoubleClick="listView1_PreviewMouseDoubleClick" SelectionMode="Single">
      <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ResourceKey=ListViewStyle}">
          <Setter Property="Foreground" > <!-- This Bit sets the foreground of the listviewitem to red if the transaction is overdue -->
            <Setter.Value>
              <MultiBinding Converter="{StaticResource myOverDue}" ConverterParameter="">
                <Binding Path="Trn_Date" />
                <Binding Path="Pay_Date" />
                <Binding Path="ServerDate" />
                <Binding Path="Balance" />
              </MultiBinding>
            </Setter.Value>
          </Setter>
          <Style.Triggers>
            <Trigger Property="IsSelected" Value="True" >
               <Setter Property="Foreground" Value="" /> <!-- Here I want to leave the foreground to Red or Black not change according to the Based on Style -->
     </Trigger>
          </Style.Triggers>
        </Style>
      </ListView.Resources>
    </ListView>

    Thursday, November 10, 2011 6:10 PM

Answers

  • Hi Ivan,
    Is it necessary that you do these things in the XAML.
    What I would suggest here is:
    <ListBox x:Name="listbox1">
    
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding flag}"  ForeGround="{Binding flag<A convertor here will determine the color here based on flag value like overdue and will return the appropriate Brush>}"></TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    This will prevent you from doing any messing up with the Listbox style as well.
    Thanks,
    Abhinav
    Please mark the solution as answered if this answers your question
    • Marked as answer by Min Zhu Friday, November 18, 2011 2:49 AM
    Thursday, November 10, 2011 6:58 PM
  • Hi Ivan,

    From your description I think the simplest solution is to use DataTrigger and your MultiBinding to set the Foreground. And it will override the original trigger setters.

                <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ResourceKey=ListViewStyle}">
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource myOverDue}" ConverterParameter="">
                                    <Binding Path="Trn_Date" />
                                    <Binding Path="Pay_Date" />
                                    <Binding Path="ServerDate" />
                                    <Binding Path="Balance" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                        <DataTrigger Value="False">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource myOverDue}" ConverterParameter="">
                                    <Binding Path="Trn_Date" />
                                    <Binding Path="Pay_Date" />
                                    <Binding Path="ServerDate" />
                                    <Binding Path="Balance" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>


    However, it would be best to remove IsSelected trigger from the base style and only add this trigger when needed.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Min Zhu Tuesday, November 15, 2011 7:00 AM
    • Marked as answer by Min Zhu Friday, November 18, 2011 2:49 AM
    Tuesday, November 15, 2011 7:00 AM

All replies

  • Hi Ivan,
    Is it necessary that you do these things in the XAML.
    What I would suggest here is:
    <ListBox x:Name="listbox1">
    
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding flag}"  ForeGround="{Binding flag<A convertor here will determine the color here based on flag value like overdue and will return the appropriate Brush>}"></TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    This will prevent you from doing any messing up with the Listbox style as well.
    Thanks,
    Abhinav
    Please mark the solution as answered if this answers your question
    • Marked as answer by Min Zhu Friday, November 18, 2011 2:49 AM
    Thursday, November 10, 2011 6:58 PM
  • Hi Ivan,

    From your description I think the simplest solution is to use DataTrigger and your MultiBinding to set the Foreground. And it will override the original trigger setters.

                <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ResourceKey=ListViewStyle}">
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource myOverDue}" ConverterParameter="">
                                    <Binding Path="Trn_Date" />
                                    <Binding Path="Pay_Date" />
                                    <Binding Path="ServerDate" />
                                    <Binding Path="Balance" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                        <DataTrigger Value="False">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource myOverDue}" ConverterParameter="">
                                    <Binding Path="Trn_Date" />
                                    <Binding Path="Pay_Date" />
                                    <Binding Path="ServerDate" />
                                    <Binding Path="Balance" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>


    However, it would be best to remove IsSelected trigger from the base style and only add this trigger when needed.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Min Zhu Tuesday, November 15, 2011 7:00 AM
    • Marked as answer by Min Zhu Friday, November 18, 2011 2:49 AM
    Tuesday, November 15, 2011 7:00 AM