First time posting a question on here, so forgive me if I make any mistakes.
I have a counter called TurnNumber. This number changes dynamically, based on buttons at the top of the screen. I want to change the colour of the background of the tab whose index equals the TurnNumber.
This is what I have at the moment
MainWindowView:
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"
xmlns:local="clr-namespace:DndBattleHelper"
mc:Ignorable="d"
Title="DnD Battle Helper" Height="450" Width="800"
xmlns:VM="clr-namespace:DndBattleHelper.ViewModels"
xmlns:Views="clr-namespace:DndBattleHelper.Views"
xmlns:Converters="clr-namespace:DndBattleHelper.Views.Converters"
Name="main">
<Window.DataContext>
<VM:MainWindowViewModel/>
</Window.DataContext>
<Window.Resources>
<Converters:TwoEqualNumbersToBooleanConverter x:Key="TwoEqualNumbersToBooleanConverter" />
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button Content="Previous Turn" Command="{Binding PreviousTurnCommand}"/>
<Button Content="Next Turn" Command="{Binding NextTurnCommand}"/>
</StackPanel>
<TabControl TabStripPlacement="Left" ItemsSource="{Binding EntitiesInInitiative}" SelectedIndex="{Binding SelectedTab}">
<TabControl.Resources>
<Style TargetType="TabItem">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource TwoEqualNumbersToBooleanConverter}" ConverterParameter="<">
<Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)" />
<Binding Path="DataContext.TurnNumber" RelativeSource="{RelativeSource AncestorType=Window}" />
</MultiBinding>
</DataTrigger.Binding>
<DataTrigger.Setters>
<Setter Property="Background" Value="Red"/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type VM:EnemyViewModel}">
<Views:EnemyView/>
</DataTemplate>
<DataTemplate DataType="{x:Type VM:PlayerViewModel}">
<Views:PlayerView/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemTemplate>
<!-- this is the header template-->
<DataTemplate>
<TextBlock
Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</StackPanel>
</Grid>
</Window>
Converter:
public class TwoEqualNumbersToBooleanConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.Length < 2)
return false;
int number1 = (int)values[0];
int number2 = (int)values[1];
// Return true if the tab index matches the TurnNumber
return number1 == number2;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
TurnNumber is passed in correctly, and I trigger the converter each time it changes. Currently it appears that I am passing the wrong value for “Tab item index”, as I call the converter 3 times, but every time the value 0 is passed in. This means that when TurnNumber = 0, then the converter actually returns true:
When TurnNumber = 0
However, it changes all of the tab items to be red, rather than just the one whose index equals TurnNumber.
I would also like this to override SelectedTab colours, so that the selected tab is the standard grey, the TurnNumber tab is red, but if the TurnNumber Tab is also the Selected Tab, then it is Red. Currently Grey overrides the red.
Thank you!
powpow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.