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

发布于 2024-10-15 06:11:01 字数 376 浏览 2 评论 0原文

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人心善变 2024-10-22 06:11:01

这是它是如何完成的。您使用 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}" />

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}" />
木格 2024-10-22 06:11:01

在此 博客文章 以及样式设置器中对 Binding 的支持是 宣布用于 SL5

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

快乐很简单 2024-10-22 06:11:01

在 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>

当然,您可以获得比这更多的创意。另外,根据定义样式的方式/时间/地点,有时在代码中完成它会更容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文