【问题标题】:Binding a listbox inside popup inside tabcontrol header在 tabcontrol 标题内的弹出窗口内绑定列表框
【发布时间】:2020-09-28 12:46:35
【问题描述】:

我正在标签页眉中创建一个弹出窗口,我想将列表绑定到弹出窗口内的列表框。但绑定不起作用。此弹窗位于选项卡控件的控件模板内。

<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>();

【问题讨论】:

  • tabItems 是在哪里定义的?
  • 绑定有什么问题?它表现如何?
  • @mm8 tabitems 定义在包含 tabcontrol 的用户控件中
  • @PeterBoone tabitems 有元素,但列表框不显示任何内容。

标签: c# wpf wpf-controls


【解决方案1】:

您可以将PopupTag 属性绑定到定义源属性的父控件,然后将ListBox 绑定到父PopupTag 属性:

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

您不能使用 RelativeSourcePopup 直接绑定到父控件。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多