我为列表框创建了以下样式,该列表框将在某些文本旁边显示图像:
<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Border">
<Grid Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image
x:Name="DisplayImage"
Source="{Binding Path=ThumbnailImage}"
Height="30" …Run Code Online (Sandbox Code Playgroud) 我重写了wpf扩展器的模板.标头包含ContentPresenter
<ContentPresenter x:Name="HeaderContent"
Grid.Column="1"
Margin="0,0,4,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
RecognizesAccessKey="True"
SnapsToDevicePixels="True"
>
<ContentPresenter.Resources>
<Style BasedOn="{StaticResource Expanderheader-Naming}"
TargetType="{x:Type TextBlock}" />
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
我试图在里面为所有TextBlocks添加我的样式.如果我将标题设置为属性,我的风格是有效的:
<Expander Header="HelloWorld">
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试以其他方式设置它时,它不会.
<Expander>
<Expander.Header>
<Grid x:Name="MyGrid">
<TextBlock>Hello Man</TextBlock>
</Grid>
</Expander.Header>
</Expander>
Run Code Online (Sandbox Code Playgroud)
如何为ContentPresenter中的任何TextBlocks设置此样式?