I need to modify one of the bound properties in my DataTemplate
. The template is defined like this (relevant portion only):
<DataTemplate x:Key="MyImageTemplate">
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource MyImageConverter}">
<Binding Path="Source" />
<Binding Path="TransparencyColor" />
<Binding Path="TransparencyTolerance" />
</MultiBinding>
</Image.Source>
</Image>
</DataTemplate>
Depending upon a condition, I need to change first binding from Source to another property Source2. Having spent the better part of the afternoon with this, I can't make the following simple code of my DataTemplateSelector
work:
Public Class MyDataTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
Dim DT = DirectCast(Application.Current.FindResource("MyImageTemplate"), DataTemplate)
Dim Control = DirectCast(DT.LoadContent(), FrameworkElement)
Dim MB = BindingOperations.GetMultiBinding(item, System.Windows.Controls.Image.SourceProperty)
Dim Source2Binding As New Binding(NameOf(ImageVM.Source2))
Dim MB2 As New MultiBinding With {.Converter = MB.Converter}
MB2.Bindings.Add(Source2Binding)
MB2.Bindings.Add(MB.Bindings(1))
MB2.Bindings.Add(MB.Bindings(2))
Control.SetBinding(System.Windows.Controls.Image.SourceProperty, MB2)
Return DT
End Function
End Class
I have confirmed that it doesn't even assign this newly created MultiBinding
to Image's Source property. What am I doing wrong?