DataContext 作为资源内转换器绑定的源
<Canvas.DataContext>
<ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
</Canvas.DataContext>
<!-- DataContext is not passed into these Instances.
they also have no knowledge of their TemplatedParent. -->
<Canvas.Resources>
<!-- is there a way to use a binding that points to the datacontext within the resources ? -->
<Converters:SomeConverter x:Key="someConverter"
SomeProperty="{Binding Path=Model.SomeProperty}" />
<!-- is there a way to point Directly to the TemplatedParent ? -->
<Converters:SomeConverter x:Key="someConverter"
SomeProperty="{TemplateBinding SomeProperty}" />
</Canvas.Resources>
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=1}" />
</Canvas>
是否可以使用使用 dataContext 或 TemplatedParent 的绑定 在 ControlTemplate 的根视觉资源中?
<Canvas.DataContext>
<ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
</Canvas.DataContext>
<!-- DataContext is not passed into these Instances.
they also have no knowledge of their TemplatedParent. -->
<Canvas.Resources>
<!-- is there a way to use a binding that points to the datacontext within the resources ? -->
<Converters:SomeConverter x:Key="someConverter"
SomeProperty="{Binding Path=Model.SomeProperty}" />
<!-- is there a way to point Directly to the TemplatedParent ? -->
<Converters:SomeConverter x:Key="someConverter"
SomeProperty="{TemplateBinding SomeProperty}" />
</Canvas.Resources>
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=1}" />
</Canvas>
is it possible to use bindings that use either the dataContext or the TemplatedParent
Within a ControlTemplate's Root Visuals resourecs ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
之前的答案非常接近。但多重绑定内的绑定应该是:
对我有用
Previous answer is reeeeally really close. but the binding inside the multibinding should be :
that worked for me
如果您希望值转换器能够访问数据上下文,您可能需要使用 ConverterParameter:然后,数据上下文将作为参数传递到您的值转换器,以实现 < a href="http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert(v=VS.100).aspx" rel="noreferrer">IValueConverter.Convert.如果您实现 IMultiValueConverter 并使用 MultiBinding 类:
元素的绑定元素传递给IMultiValueConverter 的
作为Convert
方法values
参数。Convert
具有以下签名:If you want your value converter to be able to access the datacontext you may want to use ConverterParameter instead:The datacontext will then be passed on to your value converter as a parameter to your implementation of IValueConverter.Convert.You may be able to pass bindable parameters to your valueconverter if you implement IMultiValueConverter and bind the parameters using the MultiBinding class in XAML:
The binding elements of the
<MultiBinding>
element are passed to theConvert
method ofIMultiValueConverter
as thevalues
parameter.Convert
has the following signature:点表示您绑定到数据上下文
The dot is telling that you binding to datacontext
这是一种简单而有效的方法(适用于我的应用程序):
现在您可以在
Convertor
方法中使用 (value) 作为您的DataContext
。Here is a simple and efficient way (that works for my app):
Now you can use (value) in the
Convertor
method as yourDataContext
.