微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight:如何在setter中使用一个绑定的样式(或等效的工作)

如果答案为 this question的人是正确的,那么您不能将绑定作为Silverlight中样式的设置器中的值.这是一个耻辱,因为我有4个文本框,都使用完全相同的绑定他们的Opacity属性.有没有在某种意义上“风格”他们的不透明度属性,以便所有四个人指向相同的约束力?否则,我必须单独设置每个Opacity属性.在我的情况下,它更糟糕 – 所有四个共享其他属性绑定,这意味着每个TextBlock声明是相当长的,但它们几乎是相同的(它们的属性绑定,也就是).我知道我可以简洁地在代码隐藏中设置所有共享的属性绑定,但是如果有的话,我想要一个XAML解决方案.

谢谢!

解决方法

这是怎么做的.您使用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}" />

现在,您可以随意绑定绑定到控件模板的其他属性.

这种方法可以扩展到样式:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐