I would like to put image in TabControl header using binding, but below code doesn't work. I would like to know why, and how to do it? Thanks in advance :)
1、MainWindow.xaml
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TabControl Height="300" ItemsSource="{Binding TabCollection}">
<!-- this is the header template-->
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=DisplayIndex}" />
<Image Source="{Binding Path=ContentObject.BackgroundImageSource}" Width="50"></Image>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<!-- this is the body of the TabItem template-->
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ContentObject.ShowContent}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Button Content="Button" HorizontalAlignment="Left" Margin="273,391,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
</Window>
2、MainWindow.xaml.cs
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();
}
}
3、WindowVM
public class WindowVM : INotifyPropertyChanged
{
#region Fields & Properties
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<ITestView> TabCollection { get; } = new ObservableCollection<ITestView>();
#endregion
public void Init()
{
List<ITestView> list = new List<ITestView>()
{
new Test1View(),
// new Test2View()...
};
foreach (ITestView test in list)
{
TabCollection.Add(test);
}
}
}
4、Interface
public interface ITestView
{
IContentTest ContentObject { get; set; }
int DisplayIndex { get; set; }
}
public interface IContentTest
{
string ShowContent { get; set; }
ImageSource BackgroundImageSource { get; set; }
}
5、Test1View.xaml & Test1View.xaml.cs & ContentTest1
<UserControl x:Class="WpfApp3.Test1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</UserControl>
public partial class Test1View : UserControl,ITestView
{
public Test1View()
{
InitializeComponent();
}
#region ITestView Members
public IContentTest ContentObject { get; set; } = new ContentTest1();
public int DisplayIndex { get; set; } = 1;
#endregion
}
public class ContentTest1 : IContentTest, INotifyPropertyChanged
{
public ContentTest1()
{
ShowContent = "TestAAA";
// in ResourceDictionary: <ImageSource x:Key="Image111">aaa.png</ImageSource>
BackgroundImageSource = Application.Current.TryFindResource("Image111") as ImageSource;
}
#region IContentTest Members
private string _showContent;
public string ShowContent
{
get => _showContent;
set
{
_showContent = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowContent)));
}
}
private ImageSource _backgroundImageSource;
public ImageSource BackgroundImageSource
{
get => _backgroundImageSource;
set
{
_backgroundImageSource = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BackgroundImageSource)));
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}

