Reputation: 1
I Had a Tabcontrol and a RichTextBox control as below
<TabControl x:Name="tabControl" SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}">
<TabItem Header="Edit" TabIndex="0" />
<TabItem Header="View" TabIndex="1" />
</TabControl>
<RichTextBox x:Name="richTextBox"> </RichTextBox>
and now I have two Styles defined like
<Style TargetType="Table" x:Key="EditStyleKey">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
</Style>
<Style TargetType="Table" x:Key="ViewStyleKey">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="0"/>
</Style>
I will create a table in Rich text box.
My problem is that when I select the first tabitem (SelectedIndex=0) then I should apply trigger to change the style "EditKeyStyle" for table in richtextbox. and when I select the second tab (SelectedIndex=1) then I should apply the "ViewStyleKey" for Richtextbox.
As I am new to WPF I couldn't able to fix it using the trigger and I am not sure of where to write the trigger for these kind of dependency.
Someone please provide me help for fixing this issue as this is a high priority issue.
Thanks in advance.
Upvotes: 0
Views: 911
Reputation: 1515
Consider using a single Style with Style.Triggers and DataTriggers to control the style of the table in your RichTextBox. The code below changes the BroderThickness and Padding properties based on the value of the SelectedIndex you'll have to change the SelectedIndex binding so that it points to your TabControl.SelectedIndex property.
I've used something similar to the code below in a project
<UserControl.Resources>
<Style x:Key="tableStyleKey" TargetType="Table" >
<Setter Property="BorderBrush" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedIndex}" Value="0">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=SelectedIndex}" Value="1">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<RichTextBox>
<Table Style="{StaticResource tableStyleKey}"/>
</RichTextBox>
Upvotes: 1