【问题标题】:ListBox CurrentItem / SelectedItem列表框 CurrentItem / SelectedItem
【发布时间】:2018-01-03 07:41:47
【问题描述】:

我正在使用一个自定义的 ListBoxItem,它是这样构建的

<Style x:Key="MyListBoxItem"  TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border>
                    <Border>
                        <ContentPresenter />
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是一个为我需要的编辑控件修改的基本结构,像这样

<Style x:Key="MyListBoxItemText"  TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ListBoxItem Style="{DynamicResource MyListBoxItem}">
                    <TextBox  />
                </ListBoxItem>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox 的已用 ItemsSource 的某条记录的已用 ListBoxItem 由 StyleSelector 选择。

我需要的是访问当前关注的 ListBoxItem 的可能性。我已经尝试了以下

  • 将 ListBox 的 IsSyncronizedWithCurrentItem 属性设置为 true
  • 尝试抓取 ListBox 的 SelectionChanged 事件
  • 监控ListBoxItem的IsSelected属性
  • 在 ListBoxItem 的基本样式中定义 (Multi)Trigger
  • 设置一个 EventSetter

有人可以帮帮我吗?

提前非常感谢

【问题讨论】:

  • 你能告诉我们你是如何尝试使用Trigger的吗?我会使用DataTriggerIsSelected 属性。
  • 好吧,不是真的,对不起。原因是我试图绑定/链接到 ViewModel 中的 EventHandler,这根本不起作用。但是否有可能看到,您的解决方案会是什么样子?
  • 如果没有代码示例,您希望我们如何提供帮助?当然,您可以跳过任何您想要保密的敏感信息,这是关于复制您的尝试,然后找到正确的方法。

标签: wpf styles selecteditem listboxitem currentitem


【解决方案1】:

好的,这是我的尝试:

<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding Selector.IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>


<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding ListBoxItem.IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>


<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>

在 ListBox 的样式中,IsSyncronizedWithCurrentItem 属性设置为 true。

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2021-10-08
    相关资源
    最近更新 更多