If the person who answered this question is right, you cannot put a binding as the value in a setter in a style in Silverlight. Which is a shame, because I have 4 textblocks
In Silverlight: Well... yeah, you can't do a binding. Here I used a static resource, (which probably won't meet your needs). This is closest you are going to get without doing the bindings in code.
<UserControl x:Class="SilverlightApplication1.MainPage"
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"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
Name="this" Tag="0.5">
<UserControl.Resources>
<system:Double x:Key="opacity">0.5</system:Double>
<Style TargetType="TextBlock">
<Setter Property="Opacity" Value="{StaticResource opacity}"/>
</Style>
</UserControl.Resources>
<StackPanel>
<TextBlock Text="ABC"/>
<TextBlock Text="DEF"/>
<TextBlock Text="GHI"/>
<TextBlock Text="JKL"/>
</StackPanel>
</UserControl>
EDIT: Well, here it is in WPF anyway...
Here you go, in WPF:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Name="MyWindow" Tag="0.5">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Opacity" Value="{Binding ElementName=MyWindow, Path=Tag}"/>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Text="ABC"/>
<TextBlock Text="DEF"/>
<TextBlock Text="GHI"/>
<TextBlock Text="JKL"/>
</StackPanel>
</Window>
Of course you can get a lot more creative than this. Also, depending on how / when / where your styles are defined, it is sometimes easier just to do it in code.
Check out SetterValueBindingHelper
in this blog article and support for Binding
in style setters is announced for SL5.
Here is how its done. You use a ContentControl
and specify a ControlTemplate
for it as a static resource:-
<Grid.Resources>
<ControlTemplate x:Key="CommonTextBlock" TargetType="ContentControl">
<TextBlock Opacity="{Binding SomeOpacity}" Text="{TemplateBinding Content}" />
</ControlTemplate>
<Grid.Resource>
<ContentControl Content="{Binding SomeTextValue}" Template="{StaticResource CommonTextBlock}" />
<ContentControl Content="{Binding SomeOtherTextValue}" Template="{StaticResource CommonTextBlock}" />
Now you can bung as may other properties with bindings in to the Control Template as you want.
This approach could be extended to Style
:-
<Grid.Resources>
<ControlTemplate x:Key="CommonTextBlock" TargetType="ContentControl">
<TextBlock Opacity="{Binding SomeOpacity}" Text="{TemplateBinding Content}" />
</ControlTemplate>
<Style x:Key="CommonTextBlockStyle" TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource CommonTextBlock}" />
<Setter Property="Foreground" Value="Blue" />
</Style>
<Grid.Resource>
<ContentControl Content="{Binding SomeTextValue}" Style="{StaticResource CommonTextBlockStyle}" />
<ContentControl Content="{Binding SomeOtherTextValue}" Style="{StaticResource CommonTextBlockStyle}" />
I had the exact same problem a few days ago and found the following blog post. http://blogs.msdn.com/b/delay/archive/2009/11/02/as-the-platform-evolves-so-do-the-workarounds-better-settervaluebindinghelper-makes-silverlight-setters-better-er.aspx
It works like a charm.