wpfリストボックスは個々のアイテムを太字に変更します
質問
リストボックスに入れているオブジェクトのプロパティに応じて、一部のアイテムを太字にする必要があります。
テンプレートを変更してそれを行うことができると思いますが、例を見つけることができないようです。
ありがとう!
解決
コンバータ(たとえば、IntToFontWeightConverter)を使用する場合よりも簡単に実行できます。
アイテムテンプレートを設定します:
<DataTemplate x:Key="BoldTemplate">
<TextBlock
FontWeight="{Binding Path=Position, Converter={StaticResource IntToFontWeightConverter}}"
Text="{Binding Path=Name}"
/>
</DataTemplate>
Nameは表示するもの、Positionは太字/法線の基になるプロパティです。
コンバータを作成します(太字の基になっているプロパティのタイプによって異なります)。
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
}
他のヒント
DataTriggersを使用してこれを行うことができます。 DataTriggerの簡単な使用例については、 http://manicprogrammer.com/cs/blogs/willeke/archive/2007/04/25/datatriggers-are-very-cool.aspx
ListViewを使用した例を次に示しますが、Listboxにも同じことが当てはまります。
<ListView x:Name="Notes" Margin="4,4,4,4"
ItemsSource="{Binding Path=CurrentCustomer.CustomerNotes}"
ItemTemplate="{DynamicResource CustomerNotesDataTemplate}"
Style="{DynamicResource ListViewStyle1}" />
次に、ItemTemplateは私のウィンドウのリソースにあります:
<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>
そして最後に、DataTriggersのスタイルも私のウィンドウのリソースにあります。
<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>
私の例は、おそらく必要以上に冗長ですが、アプリの1つからまっすぐ引き抜いただけです。
所属していません StackOverflow