开发者

Dynamic TabItems c# Wpf

开发者 https://www.devze.com 2023-02-22 11:05 出处:网络
I am new to WPF and I am creating an application which uses the TabControl. I am using a DataTemplateSelector and my datasource is an object I created from XML which have the properties \"type\" and \

I am new to WPF and I am creating an application which uses the TabControl. I am using a DataTemplateSelector and my datasource is an object I created from XML which have the properties "type" and "catego开发者_运维问答ryID". I select my data template based on the "type" which works fine but I also need to create a tabitem for each categoryID during runtime. My problem is currently it creates a new TabItem for each object. How do I create a new tabitem based on the categoryID and place the dataTemplate on that tab and if the tab have already been created place the DataTemplate on that tab instead of creating a new one.

Thanks in advance!!


I ended up using a CollectionViewSource with grouping and then I set the tabcontrol datacontext to the CollectionViewSource.

    private void PopulateTabControl()
    {
        DataView = (CollectionViewSource)(this.Resources["DataView"]);
        AddGrouping();            
        tabcontrol.DataContext = DataView;            
    }

    private void AddGrouping()
    {
        PropertyGroupDescription grouping = new PropertyGroupDescription();
        grouping.PropertyName = "categoryID";
        DataView.GroupDescriptions.Add(grouping);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消