【发布时间】:2017-12-04 13:11:59
【问题描述】:
我在一个窗口上有一个列表框和一个按钮。当 ListBox 获得焦点时,Button 的 IsEnabled 属性应为 True。这是我尝试过的:
<Style TargetType="{x:Type ListBox}">
<Setter
Property="Margin"
Value="15,0,15,20"></Setter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True" />
<Setter TargetName="btnActivate" Property="IsEnabled" Value="True" />
</Style.Triggers>
</Style>
这看起来很简单,但我收到一条错误消息
btnActivate 无法识别。
这样做的正确方法是什么?
编辑 1 我实现了 MoonMoo 的建议链接如下:
IsEnabled="{Binding ElementName=lstInactive, Path=SelectedIndex, Converter={StaticResource cvtr}}">
使用此转换器:
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim IsFocused As Integer = CInt(value)
Return If(IsFocused > -1, True, False)
End Function
这可以在列表框获得焦点时打开 IsEnabled,但在列表框失去焦点时不会关闭它。我想也许我必须研究一个多重绑定,它也绑定到可能获得焦点的其他控件。
编辑 2 我应该提到我尝试过:
IsEnabled="{Binding ElementName=lstInactive, Path=IsFocused, Converter={StaticResource cvtr}}">
使用转换器:
Return CBool(value)
但这并没有改变按钮的 IsEnabled 属性
【问题讨论】:
-
这里已经回答了类似的问题stackoverflow.com/questions/14068606/…。您可以改为绑定到 IsEnabled 属性。
-
@MoonMoo 查看我对 OP 的编辑。