WPF - Create reusable style with variables/parameters

后端 未结 2 611
自闭症患者
自闭症患者 2020-12-19 07:29

I have an WPF application which contains several TabItems. Each TabItem is different from each other, with a text and an icon.

This is how the TabItem´s style is def

2条回答
  •  执笔经年
    2020-12-19 08:08

    You just need to set the TabItem.DataContext to an object that contains values for your TextBlock and Image controls:

    In code:

    public class TabItemData
    {
        public string ImageSource { get; set; }
        public string Heading { get; set; }
    }
    

    In your Style in XAML:

    
        
        
    
    

    In your view model:

    public class TabControlViewModel
    {
        public TabControlViewModel()
        {
            TabItemData = new TabItemData() { Header = "Some header", 
                ImageSource = "Images/IconLeafGrey.png" };
        }
    
        public TabItemData TabItemData { get; set; }
    }
    

    In XAML:

    
        ...
    
    

    Of course, I've left out some initialisation of the data objects and the INotifyPropertyChanged interface which you should implement, but I hope you get the idea.

提交回复
热议问题