我有一个带属性的视图模型
IEnumerable<Page> Pages { get; }
Page CurrentPage { get; }
Run Code Online (Sandbox Code Playgroud)
我有像这样的XAML绑定:
<ItemsControl ItemsSource="{Binding Pages}" Margin="10">
<ItemsControl.ItemTemplate>
<DataTemplate>
<BulletDecorator>
<BulletDecorator.Bullet>
<TextBlock Text=" • " FontWeight="Heavy" />
</BulletDecorator.Bullet>
<TextBlock Text="{Binding Title}" FontWeight="{Binding ????, Converter={StaticResource BTFWC}}" />
</BulletDecorator>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
视图模型具有属性Pages和CurrentPage.我希望只要在视图模型上设置该页面,列表中页面的文本就是粗体CurrentPage.我该怎么做才能代替"????" 在XAML上面实现那个目的?我想避免在页面上有"IsCurrent"属性,因为我觉得知道它是否是当前页面并不是它的责任.
PS:BTFWC是一个"布尔到字体重量转换器",如果你想知道的话.
我希望Page不是System.Windows.Controls.Page,因为它不能放入ItemsControl.
如果不是,您可以创建IMultiValueConverter,它接受项目的页面和CurrentPage:
public class IsCurrentToFontWeightConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2 && values[0] == values[1])
{
return FontWeights.Bold;
}
return FontWeights.Normal;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
并在以下绑定中使用它:
<TextBlock Text="{Binding Title}">
<TextBlock.FontWeight>
<MultiBinding>
<MultiBinding.Converter>
<local:IsCurrentToFontWeightConverter/>
</MultiBinding.Converter>
<Binding Path="."/>
<Binding Path="DataContext.CurrentPage"
RelativeSource="{RelativeSource AncestorType={x:Type ItemsControl}}"/>
</MultiBinding>
</TextBlock.FontWeight>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)