问题描述
我正在选项卡标题中创建一个弹出窗口,我想将列表绑定到弹出窗口中的列表框。但绑定无效。此弹出窗口位于选项卡控件的控件模板内。
<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
中的直接绑定到父控件。