Silverlight: How to use a binding in setter for a

2019-01-25 06:13发布

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 that all use the exact same binding for their Opacity property. Is there anyway to in a sense "style" their Opacity property so that all four of them point to the same binding? Otherwise, I have to set each Opacity property individually. In my case it's even worse - all four share other property bindings as well, which means each TextBlock declaration is pretty dang long, and yet they're all virtually the same (their property bindings, that is). I know I could concisely set all their shared property bindings in the code-behind, but I'd like a XAML solution if there is one.

Thanks!

4条回答
成全新的幸福
2楼-- · 2019-01-25 07:00

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

查看更多
一夜七次
4楼-- · 2019-01-25 07:02

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}" />
查看更多
5楼-- · 2019-01-25 07:03

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.

查看更多
登录 后发表回答