【发布时间】:2011-06-04 03:14:02
【问题描述】:
我设计了一个模拟时钟控制。它使用两个椭圆的笔划来表示钟面的外边界和内边界。
我在 UserControl 中公开了允许用户更改这两个边框的粗细的属性。 Ellipse.StrokeThickness 属性然后绑定到这些 UserControl 属性。目前,我正在将外边框粗细的 UserControl 属性绑定到内部元素的边距,这样当边框大小增加时它们不会被隐藏。
<Ellipse Name="OuterBorder" Panel.ZIndex="1" StrokeThickness="{Binding OuterBorderThickness,
ElementName=This}" Stroke="{StaticResource OuterBorderBrush}" />
<Ellipse Name="InnerBorder" Panel.ZIndex="5" StrokeThickness="{Binding InnerBorderThickness,
ElementName=This}" Margin="{Binding OuterBorderThickness, ElementName=This}"
Stroke="{StaticResource InnerBorderBrush}">
...
<Ellipse Name="Face" Panel.ZIndex="1" Margin="{Binding OuterBorderThickness, ElementName=This}"
Fill="{StaticResource FaceBackgroundBrush}" />
...
问题在于,如果增加内边框厚度,这不会影响边距,因此小时刻度和数字可能会被部分遮挡或隐藏。所以我真正需要的是能够将内部控件的边距属性绑定到内部和外部边框厚度值的总和(它们是双精度类型)。
我已经使用 'DataContext = this;' 成功地完成了这项工作,但我正在尝试在没有这个的情况下重写控件,因为我听说不推荐这样做。我还考虑过使用转换器并将第二个值作为 ConverterParameter 传递,但不知道如何绑定到 ConverterParameter。任何提示将不胜感激。
编辑>>
感谢 Kent 的建议,我创建了一个简单的 MultiConverter 来添加输入值并返回结果。我已经将带有转换器 XAML 的 SAME 多重绑定连接到 TextBlock.Text 属性和 TextBlock.Margin 属性以对其进行测试。
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SumConverter}" ConverterParameter="Add">
<Binding Path="OuterBorderThickness" ElementName="This" />
<Binding Path="InnerBorderThickness" ElementName="This" />
</MultiBinding>
</TextBlock.Text>
<TextBlock.Margin>
<MultiBinding Converter="{StaticResource SumConverter}" ConverterParameter="Add">
<Binding Path="OuterBorderThickness" ElementName="This" />
<Binding Path="InnerBorderThickness" ElementName="This" />
</MultiBinding>
</TextBlock.Margin>
</TextBlock>
我可以看到 TexBlock 中显示的值正确,但没有设置 Margin。有什么想法吗?
编辑>>>>
有趣的是,Margin 属性可以绑定到 double 类型的数据属性,但这似乎不适用于 MultiBinding。根据 Kent 的建议,我更改了 Converter 以将值作为厚度对象返回,现在它可以工作了。谢谢肯特。
【问题讨论】:
标签: wpf sum data-binding