简体   繁体   中英

WPF Binding ListBox and TabItems

New to WPF, am trying to do something basic (I think!). I have a TabControl and a ListBox that shows what tabitems are open:

<ListBox Width="170" Height="188" ItemsSource="{Binding Items, ElementName=tabControl}" Name="ListTabs" Canvas.Left="0" Canvas.Top="27">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    El
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Is it possible to bind to specific tabitems (tabitem2 and tabitem3) rather than the whole tabcontrol? Reason being is the first tabitem1 is a welcome tab and I don't want it to be shown in the listbox.

UPDATE:

Would someone be so kind to post some code on how to use an IValueConverter to hide/filter a tabitem? I have been searching for hours with no luck. Many many thanks!

In your current set up the only way would be to run it through an IValueConverter .

    <Window.Resources>
        <converters:StripOutFirstTabConverter x:Key="StripOutFirstTabConverter"/>
    </Window.Resources>

    <ListBox Width="170" Height="188" ItemsSource="{Binding Items, ElementName=tabControl, Converter={StaticResource StripOutFirstTabConverter}}" Name="ListTabs" Canvas.Left="0" Canvas.Top="27">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    El
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
     </ListBox>

If you were willing to modify your approach you could bind the ListBox.ItemsSource to a ICollectionView and then make use of the Filter property.

public ICollectionView Tabs
{
    get 
    {
        if (_view == null)
        {
            _view = CollectionViewSource.GetDefaultView(tabControl.Items);
            _view.Filter = Filter;
        }

        return _view;
    }
}

private bool Filter(object arg)
{
    //arg will be a TabItem, return true if you want it, false if you don't
}

You would have to filter out the welcome tab so you will need to add a Filter on a CollectionView Instead of binding to the items of the tab control you'd bind to the collectionview.

Although a ValueConverter might work, I consider that a kind of hack.

您可以将转换器添加到ItemSource,然后在转换器中删除欢迎页面或进行所需的任何更改。

I recommend not doing this. Use a common data source instead with both Listbox and Tabcontrol.

To filter/intercept any data binding, you can use IValueConverter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM