在列表视图中显示打开的选项卡项名称

发布于 2024-10-11 03:29:55 字数 569 浏览 3 评论 0原文

我正在尝试在列表视图或列表框中显示打开的选项卡名称列表(建议?)。

经历了不同类型的绑定选项,我能够绑定到单个选项卡名称,但它显示垂直而不是水平。这是我的 XAML:

<ListView DockPanel.Dock="Left"
          Height="352"
          Name="listView1"
          Width="132"
          ItemsSource="{Binding ElementName=RulesTab, Path=Name}"
          IsSynchronizedWithCurrentItem="True"
          FlowDirection="LeftToRight"
          HorizontalAlignment="Left"
          HorizontalContentAlignment="Left"
          DataContext="{Binding}">

任何指针都将不胜感激,因为我希望能够看到所有打开的选项卡的列表,然后双击其中一个选项卡以使该选项卡成为焦点。非常感谢!

I'm trying to display a list of open tab names in a listview or listbox (recommendations?).

Been going through the different type of binding options and I'm able to bind to a single tab name but it displays vertical instead of horizontal. Here is my XAML:

<ListView DockPanel.Dock="Left"
          Height="352"
          Name="listView1"
          Width="132"
          ItemsSource="{Binding ElementName=RulesTab, Path=Name}"
          IsSynchronizedWithCurrentItem="True"
          FlowDirection="LeftToRight"
          HorizontalAlignment="Left"
          HorizontalContentAlignment="Left"
          DataContext="{Binding}">

Any pointers would be greatly appreciated as I'd like to be able to see a list of all the tabs open and then double click on one to bring the tab into focus. Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-10-18 03:29:55

下面是 TabControlListBox 的示例,显示其中 TabItems 的名称:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TabControl Grid.Column="0" Name="tabControl1">
        <TabItem Header="Tab1"/>
        <TabItem Header="Tab2"/>
        <TabItem Header="Tab3"/>
        <TabItem Header="Tab4"/>
    </TabControl>
    <ListBox Grid.Column="1"  ItemsSource="{Binding Items, ElementName=tabControl1}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

下面是背后的代码:

private void ListBoxItem_DoubleClick(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
    var tabItem = (TabItem)((ListBoxItem)sender).Content;
    tabControl1.SelectedItem = tabItem;
}

编辑以添加双击行为。

Here is an example of a TabControl and a ListBox showing the names of the TabItems that are in it:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TabControl Grid.Column="0" Name="tabControl1">
        <TabItem Header="Tab1"/>
        <TabItem Header="Tab2"/>
        <TabItem Header="Tab3"/>
        <TabItem Header="Tab4"/>
    </TabControl>
    <ListBox Grid.Column="1"  ItemsSource="{Binding Items, ElementName=tabControl1}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

and here's the code behind:

private void ListBoxItem_DoubleClick(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
    var tabItem = (TabItem)((ListBoxItem)sender).Content;
    tabControl1.SelectedItem = tabItem;
}

Edited to add double-click behavior.

踏月而来 2024-10-18 03:29:55

如何使用列表视图枚举选项卡控件中的选项卡的简化示例:

<TabControl Name="MyTabControl">
    <TabItem Header="Tab1">
    </TabItem>
    <TabItem Header="Tab2">
    </TabItem>
</TabControl>

<ListView  DockPanel.Dock="Left" 
           ItemsSource="{Binding ElementName=MyTabControl, Path=Items}" 
           DataContext="{Binding}">
          <ListView.ItemTemplate>
              <DataTemplate>
                  <TextBlock Text="{Binding Header}"></TextBlock>
              </DataTemplate>
          </ListView.ItemTemplate>
</ListView>

Simplified example how to enumerate the tabs in a tab control with a listview:

<TabControl Name="MyTabControl">
    <TabItem Header="Tab1">
    </TabItem>
    <TabItem Header="Tab2">
    </TabItem>
</TabControl>

<ListView  DockPanel.Dock="Left" 
           ItemsSource="{Binding ElementName=MyTabControl, Path=Items}" 
           DataContext="{Binding}">
          <ListView.ItemTemplate>
              <DataTemplate>
                  <TextBlock Text="{Binding Header}"></TextBlock>
              </DataTemplate>
          </ListView.ItemTemplate>
</ListView>
み格子的夏天 2024-10-18 03:29:55

我仍然不确定你到底想要什么,无论如何,如果需要的话可以调整。
首先,如果您绑定到特定项目,您将始终拥有一个项目,您需要将 ItemsSource 设置为集合。
假设您想要列表中所有选项卡的名称或标题,您可以将选项卡控件的 Items 设置为 ItemsSource,然后应用 ItemTemplate,一些示例代码:

<ListBox ItemsSource="{Binding ElementName=TabControlSrc, Path=Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Header}" Margin="5"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果您不使用 ItemTemplate,您将收到错误,因为同一项目只能是一个父项的可视子项。
坦率地说,这似乎有点毫无意义,因为它只是重申您的选项卡,我是否误解了什么?如果是这样,请进一步澄清。

编辑:哦哈哈,三个几乎相同的答案......

I'm still unsure as to what exactly you want, anyway, this can be adjusted if needed.
First of all if you bind to a specific item you will always have one item, you need to set ItemsSource to a collection.
Assuming you want to have the names or headers of all the tabs in your list you can set the tab control's Items as the ItemsSource and then apply a ItemTemplate, some example code:

<ListBox ItemsSource="{Binding ElementName=TabControlSrc, Path=Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Header}" Margin="5"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If you do not use the ItemTemplate you'll get an error because the same item can only be a visual child of one parent.
Frankly this seems a bit pointless since it just reiterates your tabs, did i misunderstand something? If so please clarify further.

Edit: Oh lol, three almost identical answers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文