Reputation: 61
<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 ?
Upvotes: 6
Views: 13320
Reputation: 11
SomeProperty="{Binding . ,Converter={StaticResource SomeConverter}, ConverterParameter=someParam}"
The dot is telling that you binding to datacontext
Upvotes: 1
Reputation: 123
Here is a simple and efficient way (that works for my app):
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="ColumnHeader">
<DataGridTextColumn.Binding>
<Binding Converter="{StaticResource YourConverterKey}" ConverterParameter="DataContext"/>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
Now you can use (value) in the Convertor
method as your DataContext
.
Upvotes: -3
Reputation: 3804
Previous answer is reeeeally really close. but the binding inside the multibinding should be :
<SomeFrameworkElement>
<SomeFrameworkElement.SomeProperty>
<MultiBinding Converter="{StaticResource someConverter}" >
<Binding />
</MultiBinding>
</SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>
that worked for me
Upvotes: 11
Reputation: 14956
If you want your value converter to be able to access the datacontext you may want to use ConverterParameter instead:
<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter={Binding DataContext}}" />
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:
<SomeFrameworkElement>
<SomeFrameworkElement.SomeProperty>
<MultiBinding Converter="{StaticResource someConverter}" >
<Binding Path="DataContext"/>
</MultiBinding>
</SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>
The binding elements of the <MultiBinding>
element are passed to the Convert
method of IMultiValueConverter
as the values
parameter. Convert
has the following signature:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);
Upvotes: 6