简体   繁体   中英

C# WPF Binding two ListBoxes on TabControl

I would like to have a TabControl with two TabItems and one Listbox on each Item. I want both ListBoxes to show the same content and so I bound both to the same ObservableCollection<T> . At first the items are shown correctly in Listbox1 . Also if i switch to ListBox2 the Items show up here as well. If I go back to Listbox1 afterwards all the items are gone and stay in ListBox2 . I want both ListBoxes to hold and show the same ListBoxItems constantly. Really would appreciate some help!

My XAML Code:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="350*"/>
        <RowDefinition Height="100*"/>
    </Grid.RowDefinitions>
    <Button Grid.Row="1" Margin="5,5,5,5" Content="Add" Click="Button_Click"/>
    <TabControl Grid.Row="0">
        <TabItem Header="Test1">
            <ListBox ItemsSource="{Binding Components}"/>
        </TabItem>
        <TabItem Header="Test2">
            <ListBox ItemsSource="{Binding Components}"/>
        </TabItem>
    </TabControl>
</Grid>

CS Code:

        private ListBoxItem _oBoxItem;
        private Int32 i = 0;
        private ObservableCollection<ListBoxItem> components = new ObservableCollection<ListBoxItem>();
        public ObservableCollection<ListBoxItem> Components
        {
            get
            {
                if (components == null)
                    components = new ObservableCollection<ListBoxItem>();
                return components;
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _oBoxItem = new ListBoxItem();
            _oBoxItem.Content = "Part " + i.ToString();
            Components.Add(_oBoxItem);
            i += 1;
        }

Don't add any visual elements such as ListBoxItem to the source collection. Add strings instead and you will get the expected results:

public partial class MainWindow : Window
{
    private Int32 i = 0;
    private ObservableCollection<string> components = new ObservableCollection<string>();
    public ObservableCollection<string> Components
    {
        get
        {
            if (components == null)
                components = new ObservableCollection<string>();
            return components;
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Components.Add("Part " + i.ToString());
        i += 1;
    }
}

A visual element can only appear once in the visual tree. This basically means that the ListBoxItem that you add to the first ListBox cannot be displayed in the second ListBox and vice versa.

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