Welcome to the Xceed Community | Help
Community Search  
More Search Options

Binding values from one column to another

Sort Posts: Previous Next
  •  06-24-2009, 9:56 AM Post no. 21969

    Binding values from one column to another

    How to I Binding values from one column to another with using DataTemplate? In C#, not in XAML.

     Code:

    private void GenerateDataTemplates(int ColumnNumber)
    {
        var dt = new DataTemplate();

        var spOuterFactory = new FrameworkElementFactory(typeof(StackPanel));
        spOuterFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
        spOuterFactory.SetValue(MarginProperty, new Thickness(10));

        var textBlock1 = new FrameworkElementFactory(typeof(TextBlock));
        var binding1 = new Binding();
        binding1.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof (Row), 1);
        binding1.Path = new PropertyPath(string.Format("Cells[\"{0}\"].Content", dgTemplate.Columns[ColumnNumber].FieldName));//, new object[] {});
        textBlock1.SetBinding(TextBlock.TextProperty, binding1);
       
        textBlock1.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
        spOuterFactory.AppendChild(textBlock1);

        dt.VisualTree = spOuterFactory;

        dgTemplate.Columns[ColumnNumber].CellContentTemplate = dt;
    }

  •  06-24-2009, 11:05 AM Post no. 21977 in reply to 21969

    Re: Binding values from one column to another

    I dont know about binding two columns but I wouldn't create DataTemplates in c# (its depreciated and a pain in the butt).

    I found a faster easier more natural way.

    Add a WPF XAML Resource Dictionary to your project  then in your c# set the column like so

     

    var myResourceDictionary = new MyResourceDictionary();

     column.CellContentTemplate = myResourceDictionary["DataTemplateKey"];

     

    The beauty is that the XAML ResourceDictionary has its own CodeBehind too. Very Reuseable. Very Dynamic.

    I have 20 Cell DataTemplates and CellEditors  in my ResourceDictionary useable anywhere I need them.

  •  06-24-2009, 11:08 AM Post no. 21979 in reply to 21977

    Re: Binding values from one column to another

    Why do you need to bind two Columns? What is your goal then maybe I can help accomplish it.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.