Home Forums WPF controls Xceed DataGrid for WPF how to get data for combobox from array in codebehind

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23962 |

    i have a problem with binding array with combobox in c#.

    i set the combobox row using “ControlTemplate”.

    if u have the answer pls reply me.

    regards,
    kesavan

    Imported from legacy forums. Posted by kesavan (had 615 views)

    User (Old forums)
    Member
    Post count: 23064

    Here’s some test code I’ve used to explore creating ComboBoxes in code. For my application I created a custom control, deriving from ComboBox to create a flexible ComboBox with dependency properties appropriate for my application.

    This code was created from studying other posted code on the forums and not all features of it have been tested. The production code I have is too tightly integrated with my application to be a useful example, but you should get the idea from the code below.

    I’ve also played around

    <code>
    class XceedDataGridCellEditorHelpers
    {

    static public DataTemplate CellEditorXpoComboBoxDataTemplate(string displayField, string valueField, IEnumerable collection, IValueConverter valueConverter)
    {
    /* Code adopted from Xceed.com forum posts:
    * http://forums.xceed.com/forums/ShowPost.aspx?PostID=7110
    * http://forums.xceed.com/forums/ShowPost.aspx?PostID=7936 (referencing the above post)
    */

    FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));

    if (!String.IsNullOrEmpty(displayField)) {
    comboFactory.SetValue(ComboBox.DisplayMemberPathProperty, displayField);
    }

    if (!String.IsNullOrEmpty(valueField)) {
    comboFactory.SetValue(ComboBox.SelectedValuePathProperty, valueField);
    }

    comboFactory.SetValue(ComboBox.ItemsSourceProperty, collection);

    Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension myCellEditorBinding = new Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension();

    if (valueConverter != null)
    {
    myCellEditorBinding.Converter = valueConverter;
    }

    comboFactory.SetBinding(ComboBox.SelectedItemProperty, (BindingBase)myCellEditorBinding.ProvideValue(null));
    DataTemplate template = new DataTemplate(typeof(ComboBox));
    template.VisualTree = comboFactory;
    return template;
    }

    static public Xceed.Wpf.DataGrid.CellEditor AddressComboBoxCellEditor
    {
    get
    {
    Xceed.Wpf.DataGrid.CellEditor cellEditor = new Xceed.Wpf.DataGrid.CellEditor();
    cellEditor.EditTemplate = CellEditorXpoComboBoxDataTemplate(null,null,AddressData.Addresses,new ComboBoxAddressValueConverter());
    return cellEditor;
    }
    }
    }

    ….

    public class AddressData
    {

    static private Address[] s_addresses;
    static public Address[] Addresses
    {
    get
    {

    if (s_addresses == null)
    {
    s_addresses = new Address[] {
    new Address(“14 W. 34th”,”New York”,”NY”),
    new Address(“8417 Newmont Dr.”,”Jones”,”VT”),
    new Address(“POB 321″,”Tuson”,”AZ”),
    new Address(“178 Forest Road”,”BigWoods”,”VA”)
    };
    }

    return s_addresses;
    }
    }

    }

    </code>

    You can really customize things if you want to. Check out this XAML example which you could translate into code using the above patterns if needed:

    <code>
    <xcdg:Column FieldName=”CurrentAddress” DisplayMemberBinding=”{Binding Path=CurrentAddress, Mode=TwoWay}” Title=”Address” MinWidth=”200″>

    <xcdg:Column.CellContentTemplate>
    <DataTemplate>
    <StackPanel Orientation=”Vertical”>
    <StackPanel.Resources>
    <Style TargetType=”TextBlock”>
    <Setter Property=”Margin” Value=”2″/>
    </Style>
    </StackPanel.Resources>

    <TextBlock FontStyle=”Italic” Text=”{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataCell}}, Path=DataContext.CurrentAddress.Street}” />
    <TextBlock FontStyle=”Italic” Text=”{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataCell}}, Path=DataContext.CurrentAddress.City}” />
    <TextBlock FontStyle=”Italic” Text=”{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataCell}}, Path=DataContext.CurrentAddress.State}” />
    </StackPanel>
    </DataTemplate>
    </xcdg:Column.CellContentTemplate>

    <xcdg:Column.CellEditor>
    <xcdg:CellEditor>
    <xcdg:CellEditor.EditTemplate>
    <DataTemplate>
    <ComboBox ItemsSource=”{x:Static local:AddressData.Addresses}”
    SelectedItem=”{xcdg:CellEditorBinding Converter={StaticResource ComboBoxAddressValueConverter}}”>
    <ComboBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation=”Vertical”>
    <StackPanel.Resources>
    <Style TargetType=”TextBlock”>
    <Setter Property=”Margin” Value=”2″/>
    </Style>
    </StackPanel.Resources>

    <TextBlock Text=”{Binding Path=Street}”/>
    <TextBlock Text=”{Binding Path=City}”/>
    <TextBlock Text=”{Binding Path=State}”/>
    </StackPanel>
    </DataTemplate>
    </ComboBox.ItemTemplate>
    </ComboBox>
    </DataTemplate>
    </xcdg:CellEditor.EditTemplate>

    <!–
    <xcdg:CellEditor.ActivationGestures>
    <xcdg:KeyActivationGesture SystemKey=”Down” Modifiers=”Alt”/>
    <xcdg:KeyActivationGesture Key=”Up” Modifiers=”Alt”/>
    <xcdg:KeyActivationGesture Key=”F4″/>
    <xcdg:KeyActivationGesture Key=”Space”/>
    </xcdg:CellEditor.ActivationGestures>
    –> SDJ

    Imported from legacy forums. Posted by Richard (had 856 views)

    User (Old forums)
    Member
    Post count: 23064

    Note the following correction to the posted code:

    If you send in a valueField, you’ll need to change the binding for the CellEditor:

    <code>
    if (!String.IsNullOrEmpty(valueField))
    {
    comboFactory.SetValue(ComboBox.SelectedValuePathProperty, valueField);
    comboFactory.SetBinding(ComboBox.SelectedValueProperty, (BindingBase)myCellEditorBinding.ProvideValue(null));
    }
    else
    {
    comboFactory.SetBinding(ComboBox.SelectedItemProperty, (BindingBase)myCellEditorBinding.ProvideValue(null));
    }
    </code>

    Imported from legacy forums. Posted by Richard (had 3175 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.