Silverlight:如何在样式的Setter中使用绑定(或等效的解决方法)

14
如果回答这个问题的人是正确的,那么在Silverlight中的样式的setter中,您不能将绑定作为值放置。这很遗憾,因为我有4个文本块,它们都使用完全相同的绑定来设置其不透明度属性。是否有任何方法可以在某种意义上“样式化”它们的不透明度属性,以便它们四个都指向同一个绑定?否则,我必须单独设置每个不透明度属性。在我的情况下,情况更糟 - 所有四个还共享其他属性绑定,这意味着每个TextBlock声明非常长,但它们几乎是相同的(也就是它们的属性绑定)。我知道我可以在代码后台简洁地设置它们的所有共享属性绑定,但如果有XAML解决方案,我想要一个XAML解决方案。

谢谢!


1
你可以在 WPF 的样式设置器中应用绑定,所以你只是在问 Silverlight 吗? - bartosz.lipinski
啊,我没意识到。我编辑了我的问题,明确说明是Silverlight。 - JoeCool
4个回答

11

这是完成它的方法。您可以使用 ContentControl 并为其指定静态资源的 ControlTemplate :-

<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}" />

现在,您可以在控件模板中使用绑定将其他属性添加到其中。

这种方法也可以扩展到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}" />

我喜欢这个想法,但是似乎TextBlock没有Template属性。我做错了什么吗? - JoeCool
@JoeCool:抱歉,那些“TextBlock”元素应该是内容控件。已进行编辑。(我知道我应该剪切粘贴并编辑而不是再次输入代码)。 - AnthonyWJones
哦,我一定会尝试这个。非常酷!您会建议将所有未绑定属性(例如'Foreground =“Blue”'之类的内容)单独放在样式中,而不是将它们也放在控件模板中吗?您觉得呢? - JoeCool

2

不幸的是,这仍然是一种“添加代码来解决问题”的解决方案。 - AnthonyWJones

1
在Silverlight中:嗯...是的,你不能进行绑定。在这里,我使用了静态资源(可能无法满足您的需求)。这是你在不使用代码绑定的情况下能够得到的最接近的结果。
<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>

编辑: 无论如何,这是 WPF 版本...

在这里,使用 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>

当然,你可以比这更有创意。此外,根据样式定义的方式/时间/位置,有时在代码中直接处理会更容易。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接