WPF中MultiBinding绑定Selector.IsSelectionActive附加属性传值异常求助
问题分析与解决
你遇到的Selector.IsSelectionActive始终传入DependencyProperty.UnsetValue的原因很明确:你的Binding是在Label自身上查找这个附加属性,但Selector.IsSelectionActive是Selector类(比如ListBox、ListView这类选择控件)的附加属性,Label本身不会持有这个属性的有效值。
修正方案
把Binding的RelativeSource目标改成包含该Label的Selector控件(或其具体子类,比如ListBox),而不是Label自己:
<Label x:Name="Fish" Style="{DynamicResource ItemLabel}" Content="{Binding Path=DisplayName}" Template="{DynamicResource moduleItem}" > <Label.Foreground> <MultiBinding Converter="{StaticResource DisplayStatusConv}"> <Binding Path="DisplayStatus"/> <!-- 修改AncestorType为Selector或具体的选择控件类型,比如ListBox --> <Binding Path="(Selector.IsSelectionActive)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Selector}"/> </MultiBinding> </Label.Foreground> </Label>
额外说明
- 如果你的Label是在某个具体Selector子类(比如
ListView)的ItemTemplate中,把AncestorType指定为该具体类型(如AncestorType=ListView)会更精准,避免误匹配其他嵌套的Selector。 Selector.IsSelectionActive是Selector控件自身的状态属性,当Selector获得焦点时该值为true,失去焦点时为false,无需手动赋值。
内容的提问来源于stack exchange,提问作者Isaac
相关产品推荐
相关产品推荐

