Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
695
Convert XAML style to code
posted

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);