0

I bind a ComboBox in a DataGrid with SQL data.

Xaml code

<c1:C1DataGrid Width="1119" Name="DataGridName" Background="Transparent" Cursor="Hand" CanUserAddRows="False" CanUserEditRows="True" MaxHeight="380" RowBackground="Transparent" AlternatingRowBackground="Transparent"  AutoGenerateColumns="False">
                        <c1:C1DataGrid.Columns>
                            <c1:DataGridCheckBoxColumn Binding="{Binding Selectionnee,Mode=TwoWay}" Header="Sélection" CanUserSort="True"/>
                            <c1:DataGridTextColumn Binding="{Binding Adresse}" Header="Adresse" CanUserSort="True" CanUserFilter="True" IsReadOnly="False"/>
                            <c1:DataGridTextColumn Binding="{Binding Nom}" Header="Nom" CanUserSort="True" IsReadOnly="True"/>
                            <c1:DataGridTextColumn Binding="{Binding NumMot,Mode=TwoWay}" Header="N° mot" CanUserSort="True" />
                            <strong><c1:DataGridComboBoxColumn Binding="{Binding Format , Mode=TwoWay}" Header="Format"/></strong>
                         </c1:C1DataGrid.Columns>

This part works fine.

The format values are text like this :”B1″, “B2″, “N1″, “N3″, …

Now i want to display in this ComboBox others text values corresponding to the SQL data (like an particular enum). it may be unclear, to sum up i want to display : “1.* B1″ for “B1″ value “2.* B1″ for “B2″ value “3.* B1″ for “N1″ value “4.* B1″ for “N3″ value …

So i try many things, without success.

C# code

private List<string> FormatList = new List<string>()
{
        "1.* B1",
        "2.* B2",
        "3.* N1",
        "4.* N3"
};

this.Resources.Add("FormatList", FormatList);

InitializeComponent();

Xaml code

<c1:DataGridComboBoxColumn Binding="{Binding Format , Mode=TwoWay}" Header="Format" ItemsSource="{StaticResource FormatList}"/>

i don’t know how to join Format and FormatList. I’ve tryed a DictionaryConverter but i don’t full understand it.

Can anyone help me?

Thanks

jerome

2 Answers2

0

You can do this by using a datatemplate:

            <c1:DataGridColumn Binding="{Binding Format , Mode=TwoWay}" Header="Format" >
                <ComboBox ItemsSource={Binding FormatList}>
                 <ComboBox.ItemTemplate>
                    <DataTemplate>
                       <StackPanel Orientation="Horizontal">
                      <TextBox Text="{Binding PrefixPart}" />
                      <TextBox Text="{Binding NamePart}" />
                       </StackPanel>                                                            
                    </DataTemplate>
                 </ComboBox.ItemTemplate>
                </ComboBox>
            </c1:DataGridColumn>

Then you create a class called Format

public class Format
{
    public string PrefixPart{get;set;}
    public string NamePart{get;set;}
}

And you create a IList / ObservableCollection FormatList and add to this list.

Hope that helps.

0

You have to set the selected value

[SelectedValue="{Binding Path=Format, Mode=TwoWay}"]

On my blog post your can download the code for a working example.

JBrooks
  • 9,901
  • 2
  • 28
  • 32