本文介绍了Silverlight:如何在setter中使用一个绑定的样式(或等效的工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



谢谢! >

解决方案

这是如何完成的。您使用 ContentControl 并指定一个 ControlTemplate 作为静态资源: -

 < Grid.Resources> 
< ControlTemplate x:Key =CommonTextBlockTargetType =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}/>

现在,您可以根据需要将绑定的其他属性与控件模板挂起。 p>

这种方法可以扩展到 Style : -

 < Grid.Resources> 
< ControlTemplate x:Key =CommonTextBlockTargetType =ContentControl>
< TextBlock Opacity ={Binding SomeOpacity}Text ={TemplateBinding Content}/>
< / ControlTemplate>
< Style x:Key =CommonTextBlockStyleTargetType =ContentControl>
< Setter Property =TemplateValue ={StaticResource CommonTextBlock}/>
< Setter Property =ForegroundValue =Blue/>
< / Style>
< Grid.Resource>
< ContentControl Content ={Binding SomeTextValue}Style ={StaticResource CommonTextBlockStyle}/>
< ContentControl Content ={Binding SomeOtherTextValue}Style ={StaticResource CommonTextBlockStyle}/>


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!

解决方案

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

这篇关于Silverlight:如何在setter中使用一个绑定的样式(或等效的工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:09