WPF Listbox 개별 항목을 대담하게 변경합니다
문제
내가 ListBox에 넣는 객체의 속성에 따라 일부 항목이 대담 해지기를 원합니다.
나는 당신이 템플릿을 변경하여 할 수 있다고 생각하지만 예를 찾을 수없는 것 같습니다.
감사!
해결책
컨버터 (예 : inttofontweightConverter)를 사용하는 경우 그보다 더 간단하게 수행 할 수 있습니다.
항목 템플릿 설정 :
<DataTemplate x:Key="BoldTemplate">
<TextBlock
FontWeight="{Binding Path=Position, Converter={StaticResource IntToFontWeightConverter}}"
Text="{Binding Path=Name}"
/>
</DataTemplate>
이름이 당신이 표시하고 싶은 곳이며, 위치는 대담한 / 정상을 기반으로하는 재산입니다.
컨버터를 만듭니다 (대담한 기본 속성의 유형에 따라).
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-by-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>
내 예는 아마도 필요한 것보다 훨씬 더 장점 일 것입니다. 그러나 나는 단지 내 앱 중 하나에서 바로 가져 왔습니다.
제휴하지 않습니다 StackOverflow