在tabcontrol标头内的弹出窗口中绑定一个列表框

问题描述

我正在选项卡标题中创建一个弹出窗口,我想将列表绑定到弹出窗口中的列表框。但绑定无效。此弹出窗口位于选项卡控件的控件模板内。

<Popup PlacementTarget="{Binding ElementName=ConnectionListDropDownButton}" IsOpen="{Binding ElementName=ConnectionListDropDownButton,Path=IsChecked}"   AllowsTransparency="True"   PopupAnimation="Slide"    StaysOpen="False">
    <Grid>
        <ListBox Name="ConnectionList" ItemsSource="{Binding tabItems}"  Background="White" SelectionChanged="ConnectionList_SelectionChanged" MaxHeight="300" BorderBrush="LightGray" BorderThickness="1" SelectionMode="Single" Selectedindex="-1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Popup>

这是我的列表,需要绑定。

public ObservableCollection<TabItem> tabItems { get; set; } = 
    new ObservableCollection<TabItem>();

解决方法

您可以将Tag的{​​{1}}属性绑定到定义了source属性的父控件,然后将Popup绑定到父控件的ListBox属性Tag

Popup

您不能使用<Popup PlacementTarget="{Binding ElementName=ConnectionListDropDownButton}" IsOpen="{Binding ElementName=ConnectionListDropDownButton,Path=IsChecked}" AllowsTransparency="True" PopupAnimation="Slide" StaysOpen="False" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"> <Grid> <ListBox Name="ConnectionList" ItemsSource="{Binding Tag.tabItems,RelativeSource={RelativeSource AncestorType=Popup}}" Background="White" SelectionChanged="ConnectionList_SelectionChanged" MaxHeight="300" BorderBrush="LightGray" BorderThickness="1" SelectionMode="Single" SelectedIndex="-1"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Header}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Popup> Popup中的直接绑定到父控件。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...