I have a UserControl which simply contains a TextBlock and TextBox inside a DataTemplate. This is done in the following way:
<UserControl.Resources>
<DataTemplate DataType="{x:Type Binding:StringBindingData}" x:Key="dataTemp">
<StackPanel Orientation="Horizontal" Name="sPanel">
<TextBlock Name="txtDescription" Text="{Binding Description}" />
<TextBox Name="textboxValue" Text="{Binding Mode=TwoWay, Path=Value, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ItemsControl Name="textItemsControl" ItemsSource="{Binding}"/>
</Grid>
I need to be able to apply different styles to the TextBlock/TextBox under different circumstances. For example in certain instances I would like to be able to apply a white Foreground to the TextBlock or change the width of a TextBox.
I have tried a few different approaches: In the window where the control is being used I have set the style for the TextBlock:
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="White" />
</Style>
This worked for all other TextBlocks in the window.
I also attempted to get the DataTemplate in the codebehind using
var myDataTemplate = (DataTemplate)this.Resources["dataTemp"];
But was unable to get any further in applying the style to all the TextBlock elements.