views:

1911

answers:

3

I want some of the items to be bold depending on a property of an object i'm putting into the listbox.

I think you can do it with changing templates but can't seem to find an example.

Thanks!

+4  A: 

You can do this using DataTriggers. For a simple example of using a DataTrigger you can check out http://manicprogrammer.com/cs/blogs/willeke/archive/2007/04/25/datatriggers-are-very-cool.aspx

Here's an example using a ListView, but the same thing applys to a Listbox.

<ListView x:Name="Notes" Margin="4,4,4,4" 
   ItemsSource="{Binding Path=CurrentCustomer.CustomerNotes}"
   ItemTemplate="{DynamicResource CustomerNotesDataTemplate}" 
   Style="{DynamicResource ListViewStyle1}" />

Then the ItemTemplate is in my Window's Resources:

        <DataTemplate x:Key="CustomerNotesDataTemplate">
            <Grid MinWidth="400" Style="{DynamicResource NotesRowTriggers}">
             <Grid.ColumnDefinitions>
              <ColumnDefinition Width="74"/>
              <ColumnDefinition Width="400"/>
              <ColumnDefinition Width="125"/>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="Auto"/>
             </Grid.ColumnDefinitions>
             <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Top" Text="{Binding Path=NoteDate, Converter={StaticResource ShortDateConverter}}" Margin="0,0,4,0" />
             <TextBlock Text="{Binding Path=FreeNote}" Grid.Column="1" HorizontalAlignment="Stretch" Margin="4,0,0,0" Grid.ColumnSpan="1" TextWrapping="Wrap" />
       <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Path=NoteUser}" Grid.Column="2" Width="110" d:LayoutOverrides="Width" Margin="4,0,0,0" Grid.ColumnSpan="1" />
       <CheckBox HorizontalAlignment="Left" IsChecked="{Binding Path=Highlight}" VerticalAlignment="Top" Content="Important" Grid.Column="3" Margin="4,0,4,0"/>
       <CheckBox HorizontalAlignment="Left" IsChecked="{Binding Path=Important}" VerticalAlignment="Top" Content="Alert" Grid.Column="4" Margin="4,0,4,0"/>
            </Grid>
       </DataTemplate>

And finally the Style with the DataTriggers is also in my Window's Resources.

<Style x:Key="NotesRowTriggers" TargetType="{x:Type Grid}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Important}" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Red" Opacity="0.3" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Highlight}" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Red" Opacity="0.6" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

My example is probably a lot more verbose than it needs to be, but I just pulled it straight from one of my apps.

a_hardin
+1  A: 

You can do it more simply than that if you use a converter (IntToFontWeightConverter, for example).

Set up an item template:

 <DataTemplate x:Key="BoldTemplate">
    <TextBlock
        FontWeight="{Binding Path=Position, Converter={StaticResource IntToFontWeightConverter}}"
        Text="{Binding Path=Name}" 
        />
</DataTemplate>

where Name is what you want to display, and Position is your property that you are basing the bold / normal on.

Create your converter (depending on the type of the property that you base the bold on).

class IntToFontWeightConverter :IValueConverter 
{
 #region IValueConverter Members

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
  if ((int)value == 1)
  {
   return FontWeights.Bold;
  }

  return FontWeights.Normal;
 }

 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
  throw new NotImplementedException();
 }

 #endregion
}
Donnelle
A: 

Thank you very much. It helped greatly!!