wpf - 将列表框选择的项目信息传递给用户控件(WPF)

标签 wpf user-controls listbox selecteditem

我有一个列表框,每个列表框项都是我制作的自定义用户控件。我已经使用样式删除列表框项目的所有默认突出显示(即删除所选项目的蓝色背景突出显示)。

我想要的是能够对我的用户控件做一些特殊的事情来表示列表框项目被突出显示。比如让用户控件的边框更粗一些之类的。

如果我可以在用户控件中获取一个 bool 值,我想从那里我可以弄清楚如何对用户控件进行必要的更改......通过转换器或最有可能的东西。

我不确定的是,如何将显示用户控件所在的列表框项目是否突出显示的信息传递给用户控件。

有问题的代码是这样的:

<ListBox.ItemTemplate>
 <DataTemplate>
  <hei:OrangeUserCtrl DataContext="{Binding}" Height="40" Width="40" />
 </DataTemplate>
</ListBox.ItemTemplate>

如果它所在的列表框项目被突出显示,我如何传递给用户控件(最好是 true/false)?

谢谢

最佳答案

您可以使用 Tag 属性和 RelativeSource 绑定(bind)。

在我的示例中,当项目突出显示时,我更改了边框属性(BorderBrush=RedBorderThickness=3)。

源代码:

保存数据的简单类:

class Person
{
   public string Name { get; set; }
   public string Surname { get; set; }
}

列表框:

 <ListBox ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:MyCustomPresenter DataContext="{Binding}" 
                                             Tag="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, UpdateSourceTrigger=PropertyChanged}"
                                             Height="60" Width="120" />               
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>   

显示自定义数据的用户控件:

<UserControl x:Class="WpfTextWrapping.MyCustomPresenter"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Border Margin="10">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="BorderBrush" Value="Green" />
                <Setter Property="BorderThickness" Value="1" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, UpdateSourceTrigger=PropertyChanged}" Value="True">
                        <Setter Property="BorderBrush" Value="Red" />
                        <Setter Property="BorderThickness" Value="3" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>

        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Name}" />
            <TextBlock Text="{Binding Surname}" />
        </StackPanel>        
    </Border>
</UserControl>

关于wpf - 将列表框选择的项目信息传递给用户控件(WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383226/

相关文章:

php - 从帖子中保存多个选定的列表框

c# - 尝试在 C# WPF 程序中确定 FPS

wpf usercontrol,将按钮的命令参数绑定(bind)到父用户控件

c# - 如何将 WPF 表单添加到选项卡控件的选项卡项中

c# - WPF VS Collection编辑器教程?

c# - Windows 窗体的类内 UserControl 与 UserControl 内的类

c# - 是否可以将变量绑定(bind)到集合项

c# - 开发向导 UI - WPF

wpf - 设置自动生成的列表框项的属性

c# - 无法在列表框中找到所选项目 C# ASP.NET