This is a migrated thread and some comments may be shown as answers.

Codebehind binding with Converter in C#

3 Answers 1575 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
jay
Top achievements
Rank 1
jay asked on 05 Jun 2011, 05:48 PM
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}}" ] ?

3 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 06 Jun 2011, 07:27 AM
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
0
jay
Top achievements
Rank 1
answered on 06 Jun 2011, 11:53 PM
THANKS !
0
Tingting
Top achievements
Rank 1
answered on 16 Nov 2012, 03:18 PM
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.  
Tags
GridView
Asked by
jay
Top achievements
Rank 1
Answers by
Maya
Telerik team
jay
Top achievements
Rank 1
Tingting
Top achievements
Rank 1
Share this question
or