silverlight自定义控件中转换器的绑定问题
我这里有一些xaml,我想做的只是在矩形的宽度上绑定一个属性调用Property(不是真实名称),并使用转换器名称Conv转换该属性的值,它与{TemplateBinding完美配合Property} 或 DataContext={TemplateBinding Property} 或使用相对源(如代码示例中所示)。
我的问题是转换器参数也应该是一个绑定属性,但我无法绑定转换器参数中的任何属性。因此示例中的 30 应该类似于 {Binding Path=SecondProperty}。如果有人遇到这个问题,或者如果有人有其他方法在自定义控件中绑定内容,非常感谢;)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:RatingControl">
<Style TargetType="controls:Ctr">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Ctr">
<Grid>
<Grid.Resources>
<controls:Converter x:Name="Conv" />
</Grid.Resources>
<Rectangle x:Name="rect" Width="{Binding Path=Property, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Conv}, ConverterParameter=30}" Height="20" />
i got some xaml here and what i m trying to do it's simply bind a property call Property (not the real name) on the width of a rectangle and to convert the value of this property with the converter name Conv and it's working perfectly with {TemplateBinding Property} or DataContext={TemplateBinding Property} or with a relative source (like in the code sample).
My problem is that the converterParameter should also be a binding property, but i m not able to bind any property in the converterParameter. So the 30 in the sample should be something like {Binding Path=SecondProperty}. If anyone got that problem or maybe if anyone got some other way to bind stuff in custom control thanks a lot ;)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:RatingControl">
<Style TargetType="controls:Ctr">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Ctr">
<Grid>
<Grid.Resources>
<controls:Converter x:Name="Conv" />
</Grid.Resources>
<Rectangle x:Name="rect" Width="{Binding Path=Property, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Conv}, ConverterParameter=30}" Height="20" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来不可能: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converterparameter(VS.95).aspx
It doesn't look like that's possible: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converterparameter(VS.95).aspx
您可以向 Converter 类添加属性并绑定到该属性。
You can add a property to the Converter class and bind to that.
您无法绑定到
Binding
对象的属性,因为它不是DependencyProperty
,事实上 Binding 也不是DependencyObject
。这是可以理解的,您可以想象管理依赖关系树的复杂性以及绑定中递归或循环绑定的可能性。但是,您可以使用专用转换器来完成该任务:-
现在您的 Xaml 看起来像:-
You can't bind to a property of the
Binding
object, since it isn't aDependencyProperty
in fact Binding isn't aDependencyObject
. This is understandable can you imagine the complexity of managing dependency trees and the possiblity of recursive or circular bindings in bindings.However you could use a Specialised converter for the task:-
now your Xaml looks like:-
这是一个非常好的解决方案,但它不起作用,因为我的第一个属性必须绑定(双向),因为如果我对其进行任何更改,转换器必须再次转换该值,以便我返回结果并显示真实结果。
It's a really good solution but it's not working bcs my first property must be bind (twoWay) because if i got any change on it the converter must convert again the value so i get the result back and show the real result.