Codebehind binding with Converter in C#

4 posts, 1 answers
  1. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 05 Jun 2011 Link to this post

    Because I wanted to use multiple instances of the same control I decided to move the below implementation (which works) from xaml to codebehind.
    <telerik:GridViewDataColumn
    Header="Duration"
    DataMemberBinding="{Binding Converter={StaticResource DurationConverter}}"
    Width="85"/>

    public class DurationConverter: IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    return ((myEntity)value).DurationValue + " " + ((myEntity)value).durationType;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

    This converter combines two fields to give me something like "3 Weeks" or "5 days"
    Now, in my codebehind, I have the following which works for "normal" GridViewDataColumns but not for columns that bind with a static converter.
    private void rgvEmpSubControl_Loaded(object sender, RoutedEventArgs e)
    {
    GridViewDataControl dataControl = (GridViewDataControl)sender;
    dataControl.Columns.Clear();
    bnd = new Binding();
    bnd.Mode = BindingMode.TwoWay;
    bnd.Path = new PropertyPath("SelectedItem");
    bnd.Source = _viewModel.EmpTrainings;
    dataControl.Columns.Add(DataColummParameters("Duration", "abc", 85));
    dataControl.SetBinding(GridViewDataControl.SelectedItemProperty, bnd);
    }
    private GridViewDataColumn DataColummParameters(string _Header, string _Binding, int intWidth)
    {
    GridViewDataColumn GVDC = new GridViewDataColumn();
    GVDC.DataMemberBinding = new Binding(_Binding);
    GVDC.Header = _Header;
    GVDC.Width = intWidth;
    return GVDC;
    }
    My questions is
    how can I structure "abc" to produce the codebehind equivalent of [DataMemberBinding="{Binding Converter={StaticResource DurationConverter}}" ] ?
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Jun 2011 Link to this post

    Hello jay,

    You may set the converter in the code-behind as follows:

    GridViewDataColumn column = new GridViewDataColumn();
    column.DataMemberBinding = new Binding("Gender"){Converter = new GenderConverter()};           
    this.playersGrid.Columns.Add(column);
     

    Kind regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. jay
    jay avatar
    39 posts
    Member since:
    Jun 2010

    Posted 06 Jun 2011 Link to this post

    THANKS !
  4. Tingting
    Tingting avatar
    14 posts
    Member since:
    May 2012

    Posted 16 Nov 2012 Link to this post

    Hello, 

     Here is my code xaml.  


    <telerik:GridViewDataColumn TextAlignment="Left" IsReadOnly="True"
                    Header="Etat" HeaderTextAlignment="Center" Width="*" UniqueName="etat">
          <telerik:GridViewDataColumn.CellTemplate>
              <DataTemplate x:Name="etatCell">
                <StackPanel>
                   <Image x:Name="imageEtat" Source="{Binding Mode=OneWay, Converter={StaticResource EtatToImageConverter}}" Width="50" Height="50" />
               </StackPanel>
             </DataTemplate>
          </telerik:GridViewDataColumn.CellTemplate>
    </telerik:GridViewDataColumn>


      I want to know how to bind the converter whitch is in the 
    DataTemplate  in the code behind.  
Back to Top