<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 resourecs?
Если вы хотите, чтобы ваш преобразователь значений имел доступ к тексту данных, вы можете вместо этого использовать ConverterParameter:
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter={Binding DataContext}}" />
Затем текст данных будет передан в преобразователь значений в качестве параметра для вашего реализация IValueConverter.Convert .
Вы можете передать привязываемые параметры в свой преобразователь значений, если вы реализуете IMultiValueConverter и свяжете параметры с помощью класса MultiBinding в XAML:
<SomeFrameworkElement>
<SomeFrameworkElement.SomeProperty>
<MultiBinding Converter="{StaticResource someConverter}" >
<Binding Path="DataContext"/>
</MultiBinding>
</SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>
Элементы привязки Элемент
передается методу Convert
из IMultiValueConverter
в качестве параметра values
. Convert
имеет следующую сигнатуру:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);