Hi,@Sherry Shi. Welcome to Microsoft Q&A. Based on the problem description that you want to use binding to put an image into the TabControl header, I completed the example below. If you want to achieve the following effect, you could refer to its code. If I misunderstood, please let me know.
MainWinow.xaml:
<Grid>
<TabControl Height="300" ItemsSource="{Binding TabCollection}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding MyHeader}" />
<Image Source="{Binding ImageHeader}" Width="50"></Image>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyContents}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Button Content="Button" HorizontalAlignment="Left" Margin="273,391,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media.Imaging;
namespace TabControlDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
vm = new WindowVM();
this.DataContext = vm;
InitializeComponent();
}
private WindowVM vm;
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.Init();
}
}
public class WindowVM : INotifyPropertyChanged
{
public ObservableCollection<MyContent> TabCollection { get; } = new ObservableCollection<MyContent>();
public void Init()
{
List<MyContent> list = new List<MyContent>();
list.Add(new MyContent(){ MyHeader="Tab 1" , ImageHeader= new BitmapImage(new Uri("spring.jpg", UriKind.Relative)), MyContents="Content 1" });
list.Add(new MyContent(){ MyHeader="Tab 2" , ImageHeader= new BitmapImage(new Uri("images.jpg", UriKind.Relative)), MyContents="Content 2" });
list.Add(new MyContent(){ MyHeader="Tab 3" , ImageHeader= new BitmapImage(new Uri("spring1.jpg", UriKind.Relative)), MyContents="Content 3" });
foreach (MyContent test in list)
{
TabCollection.Add(test);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class MyContent : INotifyPropertyChanged
{
public string myHeader;
public string MyHeader
{
get { return myHeader; }
set
{
myHeader = value;
OnPropertyChanged();
}
}
public BitmapImage imageHeader;
public BitmapImage ImageHeader
{
get { return imageHeader; }
set
{
imageHeader = value;
OnPropertyChanged();
}
}
public string myContents ;
public string MyContents
{
get { return myContents; }
set
{
myContents = value;
OnPropertyChanged();
}
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
The result:
Update:
I defined two interfaces: ITestView and IContentTest, as well as concrete classes that implement these interfaces. These classes define the content and properties of the tabs in the TabControl.
MainWindow.xaml:
<Grid>
<TabControl Height="300" ItemsSource="{Binding TabCollection}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding DisplayIndex}" />
<Image Source="{Binding ContentObject.BackgroundImageSource}" Width="50"></Image>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding ContentObject.ShowContent}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Button Content="Button" HorizontalAlignment="Left" Margin="273,391,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media.Imaging;
namespace TabControlInerface
{
public partial class MainWindow : Window
{
public MainWindow()
{
vm = new WindowVM();
this.DataContext = vm;
InitializeComponent();
}
private WindowVM vm;
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.Init();
}
}
public class WindowVM
{
public ObservableCollection<Con> TabCollection { get; } = new ObservableCollection<Con>();
public void Init()
{
List<Con> list = new List<Con>() { new Con() };
foreach (Con test in list)
{
TabCollection.Add(test);
}
}
}
public interface ITestView
{
IContentTest ContentObject { get; set; }
int DisplayIndex { get; set; }
}
public interface IContentTest
{
string ShowContent { get; set; }
BitmapImage BackgroundImageSource { get; set; }
}
public class MyContent :IContentTest
{
public string ShowContent { get; set; }
public BitmapImage BackgroundImageSource { get; set; }
public MyContent()
{
ShowContent = "header1";
BackgroundImageSource = new BitmapImage(new Uri("spring.jpg", UriKind.Relative));
}
}
public class Con : ITestView
{
public IContentTest ContentObject { get; set; } = new MyContent();
public int DisplayIndex { get; set; } = 1;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.