本文介绍了如何访问在样式父控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的列表视图具有datatemplated作为标签的物品。我设计了该标签的风格,我不知道如何访问父(ListViewItem的)IsSelected属性。
编辑 - 尝试下面的建议,但仍得到一个例外,这是我完全code:
<风格X:键=ListViewItemStyle的TargetType =ListViewItem的>
< setter属性=SnapsToDevicePixelsVALUE =真/>
< setter属性=OverridesDefaultStyleVALUE =真/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =ListViewItem的>
< BORDER BorderBrush ={TemplateBinding BorderBrush}了borderThickness ={TemplateBinding了borderThickness}后台={TemplateBinding背景}填充={TemplateBinding填充}SnapsToDevicePixels =真>
< GridViewRow presenter的HorizontalAlignment ={TemplateBinding HorizontalContentAlignment}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}VerticalAlignment ={TemplateBinding VerticalContentAlignment}/>
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< Style.Triggers>
<触发属性=IsSelectedVALUE =真>
< setter属性=背景VALUE ={StaticResource的WindowBorderBrush}/>
< setter属性=前景VALUE =白/>
< /触发>
< /Style.Triggers>
< /样式和GT;
<风格X:键=GVLabelStyle
支持算法FMP ={StaticResource的LabelStyle}
的TargetType =标签>
< Style.Triggers>
< MultiDataTrigger>
< MultiDataTrigger.Conditions>
<条件属性={绑定路径= IsSelected,的RelativeSource = {的RelativeSource FindAncestor}}VALUE =真/>
< /MultiDataTrigger.Conditions>
< setter属性=前景VALUE =白/>
< / MultiDataTrigger>
< /Style.Triggers>
< /样式和GT; <的DataTemplate X:键=appTemplate>
<标签样式={StaticResource的GVLabelStyle}
CONTENT ={结合ProcessInfo.ProcessName}>
< /标签>
< / DataTemplate中> < ListView的背景=透明
NAME =mainContentHolder
的ItemsSource ={结合}
BorderBrush =透明
ItemContainerStyle ={StaticResource的ListViewItemStyle}>
< ListView.View>
< GridView的ColumnHeaderContainerStyle ={StaticResource的HeaderStyle}>
< GridViewColumn标题=应用程序
CellTemplate ={StaticResource的appTemplate}/>
< GridViewColumn标题=窗口标题
CellTemplate ={StaticResource的wndTemplate}
WIDTH =300/>
< GridViewColumn标题=日期
CellTemplate ={StaticResource的dateTemplate}/> < / GridView的>
< /ListView.View> < /&的ListView GT;
解决方案
您应该能够使用的:
<条件属性={绑定路径= IsSelected,的RelativeSource = {的RelativeSource TemplatedParent}}VALUE =真/>
编辑:
尝试使用 MultiDataTrigger
,而不是 MultiTrigger
为好。检查this.
My Listview has items datatemplated as a label. I'm designing a style for that label and I don't know how to access the parent's(ListViewItem) IsSelected property.
EDIT - tried the suggestions below, but still getting an exception, here's my complete code:
<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>
解决方案
You should be able to use RelativeSource:
<Condition Property="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}" Value="True" />
EDIT:Try a using a MultiDataTrigger
instead of a MultiTrigger
as well. Check this.
这篇关于如何访问在样式父控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!