I have a style in XAML which works fine but now I have to create this style in the code behind. My problem is with setting the binding to the value of templated parent which is the CellValuePresenter(<Binding Path="Value" RelativeSource="{RelativeSource TemplatedParent}"/>) This value is being passed as null to my ValueConverter. The other bound value is being passed fine. How can I do this in code behind? I've pasted relevant code sections below.
the XAML
//<Style x:Key="cvpStyle" TargetType="{x:Type igDP:CellValuePresenter}"> //<Setter Property="Template"> // <Setter.Value> // <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> // <Rectangle Name="myRectangle"> // <Rectangle.Fill> // <VisualBrush> // <VisualBrush.Visual> // <Label> // <Label.Content> // <MultiBinding Converter="{StaticResource cvpValueChangeConverter}"> // <Binding Path="Value" RelativeSource="{RelativeSource TemplatedParent}"/> // <Binding Path="SelectedItem" ElementName="lbOfferBidFormat" />
the C# code (the binding section)
MultiBinding multiBinding = new MultiBinding(); multiBinding.Converter = FindResource("cvpValueChangeConverter") as IMultiValueConverter;
Binding binding1 = new Binding(); binding1.Path = new PropertyPath("Value"); binding1.RelativeSource = RelativeSource.TemplatedParent;
Binding binding2 = new Binding(); binding2.Source = ((Main)App.Current.MainWindow).lbOfferBidFormat; binding2.Path = new PropertyPath("Text"); //SelectedItem
multiBinding.Bindings.Add(binding1); multiBinding.Bindings.Add(binding2);
What are you setting this binding on, are you creating a Style in code using FrameworkElementFactorys? Can you post the rest of the code that makes use of this binding?
The code is below. The multibinding is working fine as I am hitting the custom converter and the value from the combo box is being passed fine but the value from the CellValuePresenter is coming in as UnsetValue. below is the full binding code. When I had this style as a XAML resource and then associate it via code it works fine.
Style style = new Style(typeof(CellValuePresenter)); ControlTemplate ct = new ControlTemplate(typeof(CellValuePresenter));
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Rectangle));
//Create binding MultiBinding multiBinding = new MultiBinding(); multiBinding.Converter = FindResource("cvpValueChangeConverter") as IMultiValueConverter; //1st binding Binding binding1 = new Binding(); binding1.Path = new PropertyPath("Value"); binding1.RelativeSource = RelativeSource.TemplatedParent;
//2n biding Binding binding2 = new Binding(); binding2.Source = ((Main)App.Current.MainWindow).lbOfferBidFormat; binding2.Path = new PropertyPath("Text"); //SelectedItem
Label label = new Label(); label.Foreground = Brushes.Black; label.FontSize = 10d; label.FontFamily = new FontFamily("Tahoma"); label.VerticalAlignment = VerticalAlignment.Center; label.FontStretch = FontStretches.ExtraCondensed;
label.SetBinding(Label.ContentProperty, multiBinding);
VisualBrush visualBrush = new VisualBrush(label);
fef.SetValue(Rectangle.FillProperty, visualBrush);
ct.VisualTree = fef;
style.Setters.Add(new Setter(Control.TemplateProperty, ct));