gpt4 book ai didi

c# - 如何以一种样式访问控件父级的属性

转载 作者:行者123 更新时间:2023-11-30 22:18:14 25 4
gpt4 key购买 nike

我的 ListView 将项目数据模板化为标签。我正在为该标签设计样式,但我不知道如何访问父项的 (ListViewItem) IsSelected 属性。

编辑 - 尝试了以下建议,但仍然出现异常,这是我的完整代码:

 <Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource WindowBorderBrush}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="GVLabelStyle"
BasedOn="{StaticResource LabelStyle}"
TargetType="Label">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor}}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="White"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>

<DataTemplate x:Key="appTemplate">
<Label Style="{StaticResource GVLabelStyle}"
Content="{Binding ProcessInfo.ProcessName}">
</Label>
</DataTemplate>

<ListView Background="Transparent"
Name="mainContentHolder"
ItemsSource="{Binding}"
BorderBrush="Transparent"
ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
<GridViewColumn Header="Application"
CellTemplate="{StaticResource appTemplate}"/>
<GridViewColumn Header="Window Title"
CellTemplate="{StaticResource wndTemplate}"
Width="300"/>
<GridViewColumn Header="Date"
CellTemplate="{StaticResource dateTemplate}"/>

</GridView>
</ListView.View>

</ListView>

最佳答案

您应该能够使用 RelativeSource :

<Condition Property="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}" Value="True" />

编辑:也尝试使用 MultiDataTrigger 而不是 MultiTrigger。检查this .

关于c# - 如何以一种样式访问控件父级的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16230616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com