在我的 WPF 應用程式(.NET 6.0)中,我有一個ItemContainerStylefor aListBoxItem我已經在ListBoxes 上成功使用了一段時間。但就在今天,我嘗試ListBox在我也設定ItemTemplateSelector屬性的地方使用它。我發現我的風格導致選擇器被簡單地忽略。
除錯器輸出視窗中沒有警告或錯誤訊息,也沒有拋出例外。只是該SelectTemplate方法永遠不會被呼叫。所以我最終得到了一個裝滿文本類名稱的框。但是,如果我ItemContainerStyle從 中洗掉ListBox,一切正常,并且SelectTemplate為每個專案呼叫。但后來我ListBoxItem的 s 看起來不像我想要的那樣。
下面是我的ItemContainerStyle。只要我ItemContainerStyle在 ListBox 上使用它,我就可以輕松地在測驗應用程式中重現相同的效果。任何人都可以找出可能導致ItemTemplateSelector簡單忽略的內容嗎?
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="3,0,3,0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Foreground" Value="{StaticResource GsForegroundLight}" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="Local" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="1"
SnapsToDevicePixels="True" />
<!-- MouseOver Visual is exactly like Selected Visual (below) minus the rounded corners -->
<Border
x:Name="MouseOverVisual"
BorderBrush="{StaticResource GsForegroundLight}"
BorderThickness="1"
CornerRadius="3"
Opacity="0"
SnapsToDevicePixels="True">
<Border BorderThickness="1" CornerRadius="3" SnapsToDevicePixels="True">
<Border.Background>
<LinearGradientBrush Opacity="0.5" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="1" Color="{StaticResource GsBackgroundDarkColor}" />
<GradientStop Color="Gray" />
</LinearGradientBrush>
</Border.Background>
</Border>
</Border>
<!-- Selected Visual uses no border but just a gradient background brush -->
<Border x:Name="SelectedVisual"
BorderThickness="0"
CornerRadius="3"
Opacity="0"
SnapsToDevicePixels="True">
<Border
BorderBrush="Black"
BorderThickness="1"
CornerRadius="3"
SnapsToDevicePixels="True">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="1" Color="{StaticResource GsBackgroundDarkColor}" />
<GradientStop Color="Gray" />
</LinearGradientBrush>
</Border.Background>
</Border>
</Border>
<ContentPresenter x:Name="contentPresenter"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
<!-- JMO: Removed the focus visual altogether by setting border thickness to zero -->
<Border x:Name="FocusVisual"
BorderBrush="#FFFFC92B"
BorderThickness="0"
CornerRadius="1"
Opacity="0"
SnapsToDevicePixels="True" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MouseOverVisual"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="contentPresenter"
Storyboard.TargetProperty="Opacity"
To="0.5"
Duration="0" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="SelectedVisual"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="FocusVisual"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
uj5u.com熱心網友回復:
ContentPresenter 本身從 ContentControl 接收資料。您已為其屬性??設定系結。也許這就是為什么這個自動機制對他關閉的原因。
嘗試像在默認 ListBoxItem 模板中那樣設定它。
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527026.html
上一篇:如何更改行背景的顏色?c#
