Silverlight: How to use a binding in setter for a style (or an equivalent work around)

前端 未结 4 883
臣服心动
臣服心动 2020-12-17 15:26

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

相关标签:
4条回答
  • 2020-12-17 16:02

    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.

    0 讨论(0)
  • 2020-12-17 16:06

    Check out SetterValueBindingHelper in this blog article and support for Binding in style setters is announced for SL5.

    0 讨论(0)
  • 2020-12-17 16:17

    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}" />
    
    0 讨论(0)
  • 2020-12-17 16:23

    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.

    0 讨论(0)
提交回复
热议问题