Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将listbox所选项目信息传递给usercontrol(WPF)_Wpf_User Controls_Listbox_Selecteditem - Fatal编程技术网

将listbox所选项目信息传递给usercontrol(WPF)

将listbox所选项目信息传递给usercontrol(WPF),wpf,user-controls,listbox,selecteditem,Wpf,User Controls,Listbox,Selecteditem,我有一个listbox,每个listbox项都是我创建的自定义usercontrol。我已使用样式删除列表框项目的所有默认高亮显示(即删除选定项目的蓝色背景高亮显示) 我想要的是能够对我的用户控件执行一些特殊的操作,以指示高亮显示listbox项。例如,使用户控件上的边框更加粗体,诸如此类 如果我能在用户控件中加入一个布尔值,我想从那里我就能知道如何对用户控件进行必要的更改。。。通过转换器或最有可能的东西 我不确定的是,如何将显示用户控件所在的listbox项是否高亮显示的信息传递给userco

我有一个listbox,每个listbox项都是我创建的自定义usercontrol。我已使用样式删除列表框项目的所有默认高亮显示(即删除选定项目的蓝色背景高亮显示)

我想要的是能够对我的用户控件执行一些特殊的操作,以指示高亮显示listbox项。例如,使用户控件上的边框更加粗体,诸如此类

如果我能在用户控件中加入一个布尔值,我想从那里我就能知道如何对用户控件进行必要的更改。。。通过转换器或最有可能的东西

我不确定的是,如何将显示用户控件所在的listbox项是否高亮显示的信息传递给usercontrol

所讨论的代码如下所示:

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

如果它所在的列表框项高亮显示,我如何传递到用户控件(最好是真/假)


谢谢

如果我很了解您,您需要向绑定到嵌套的
组合框的自定义
UserControl
添加一个属性,例如:

  public object MySelectedItem
    {
        get { return myNestedCombox.SelectedItem; }
        set { myNestedCombox.SelectedItem = value; }
    }

您还需要
NotifyPropertyChanged

您可以使用
Tag
属性和相对资源绑定

在我的示例中,当项目高亮显示时,我更改了边框属性(
BorderBrush=Red
BorderThickness=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:

<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>