绑定中的 silverlight 表达式
silverlight expression in binding
我正在设计我的用户控件。
我有一些关于对象大小的"constants",在我的模板对象中我有类似
的东西
<UserControl ...>
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
...
<ControlTemplate ...>
<Grid x:Name="width_plus_margin">
...
如果我希望 "witdh_plus_margin" 的宽度为 "width" 值,我只需添加类似
的内容
Width="{StaticResource width}"
但我真正需要的是设置类似
的东西
Width="{StaticResource width} + {StaticResource margin}"
这个语法是错误的。有没有办法指定我需要什么?
您不能在一个绑定中绑定到多个来源 属性。因此,您需要某种聚合器来提供可以绑定的输出 属性。
以下是同一模式的一些变体:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
<BindableResult x:Key="widthPlusMargin" ArithmeticOperation="Add" LeftOperand="{StaticResource width}" RightOperand="{StaticResource margin}"/>
</UserControl.Resources>
<Grid Width="{StaticResource widthPlusMargin}">
BindableResult 具有要加倍的隐式转换运算符:
public static implicit operator double(BindableResult source)
{
return source.InternalResult;
}
或类似这样的内容:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
<Grid>
<i:Interaction.Behaviors>
<SetCombinedWidth Value1="{StaticResource width}" Value2="{StaticResource margin}"/>
</i:Interaction.Behaviors>
</Grid>
您还可以 google silverlight 多绑定实现 看看是否更合您的口味。但最终它只是聚合器的另一种变体。
我正在设计我的用户控件。
我有一些关于对象大小的"constants",在我的模板对象中我有类似
的东西<UserControl ...>
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
...
<ControlTemplate ...>
<Grid x:Name="width_plus_margin">
...
如果我希望 "witdh_plus_margin" 的宽度为 "width" 值,我只需添加类似
的内容Width="{StaticResource width}"
但我真正需要的是设置类似
的东西Width="{StaticResource width} + {StaticResource margin}"
这个语法是错误的。有没有办法指定我需要什么?
您不能在一个绑定中绑定到多个来源 属性。因此,您需要某种聚合器来提供可以绑定的输出 属性。
以下是同一模式的一些变体:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
<BindableResult x:Key="widthPlusMargin" ArithmeticOperation="Add" LeftOperand="{StaticResource width}" RightOperand="{StaticResource margin}"/>
</UserControl.Resources>
<Grid Width="{StaticResource widthPlusMargin}">
BindableResult 具有要加倍的隐式转换运算符:
public static implicit operator double(BindableResult source)
{
return source.InternalResult;
}
或类似这样的内容:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
<Grid>
<i:Interaction.Behaviors>
<SetCombinedWidth Value1="{StaticResource width}" Value2="{StaticResource margin}"/>
</i:Interaction.Behaviors>
</Grid>
您还可以 google silverlight 多绑定实现 看看是否更合您的口味。但最终它只是聚合器的另一种变体。