<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> </Border> <Rectangle Height="1" Fill="Gray" Margin="0,0,0,5"> <Rectangle.Style> <Style TargetType="Rectangle"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </Rectangle.Style> </Rectangle> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>中如何让Rectangle和ItemsControl的字体颜色保持一致
时间: 2024-02-14 10:08:58 浏览: 111
MSDN_Magazine_2010.06(MSDN杂志)
你可以将Rectangle的Fill属性绑定到ItemsControl的Foreground属性上,这样就可以让Rectangle和ItemsControl的字体颜色保持一致了。
具体代码如下:
```xml
<Rectangle Height="1" Margin="0,0,0,5" Fill="{Binding Foreground, ElementName=itemCtrl}">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Visibility" Value="Collapsed" />
<<Style.Triggers>
<DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
```
这里将Rectangle的Fill属性绑定到了ItemsControl的Foreground属性上,通过ElementName指定了ItemsControl的名称。然后在Style.Triggers中设置了DataTrigger,当ShowUnderline属性为True时显示Rectangle,否则隐藏。
阅读全文