我想在 XAML 中使用触发器执行此操作,我该怎么做?
If ListBox1.SelectedIndex > -1 Then
Border1.Visibility = Windows.Visibility.Visible
Else
Border1.Visibility = Windows.Visibility.Hidden
End If
此 XAML 代码不起作用。 SelectedIndex
成员无效,因为它没有限定类型名称。
<ListBox.Triggers>
<Trigger SourceName="ListBox1" Property="SelectedIndex" Value="False">
<Setter TargetName="Border1" Property="Visibilty" Value="Hidden" />
</Trigger>
</ListBox.Triggers>
最佳答案
你能告诉我你是如何尝试在 xaml 中做到这一点的吗?
如果出现此错误消息,您还需要在触发器内的属性中提及类型名称。
<Trigger SourceName="ListBox1" Property="ComboBox.SelectedIndex" Value="-1">
<Setter TargetName="Border1" Property="Border.Visibility" Value="Hidden" />
</Trigger>
此外,您似乎在 <ListBox.Triggers>
中添加了一个触发器集合,但您只能将 EventTrigger 添加到该集合中。因此,您需要为 ListBox 声明样式以为其添加触发器,并且 Border 元素应该在 ListBox 的 ControlTemplate 内,但在您的情况下,Border 似乎在 ListBox 之外,因此声明样式不是解决方案。相反,您应该在 ValueConverter(比如 IndexToVisibilityConverter)的帮助下使用 Binding with SelectIndex 属性。您需要在代码隐藏中定义此转换器并将其添加到资源中。
<Border Visibility={Binding Path=SelectedIndex, ElementName=ListBox1, Converter={StaticResource IndexToVisibilityConverter}}/>
选择完全取决于您的要求。
关于wpf - 我如何在 XAML 中执行此操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1965274/