4

I am trying to use a DataGrid whose first column is a ComboxBox. This ComboxBox has a hardcoded static values, possible values are: Employee, Contractor, Supplier

How can I show some static values in the DataGrid without binding to a DataSource. I am new to WPF so more detailed explanation would help.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
VNarasimhaM
  • 1,460
  • 3
  • 22
  • 36

2 Answers2

13

if you mean the wpf toolkit datagrid, you could do it like so:

        <dg:DataGridComboBoxColumn 
           Header="String Column" 
           SelectedItemBinding="{Binding Path=RoleProperty}">
           <dg:DataGridComboBoxColumn.ItemsSource>
              <CompositeCollection>
                 <system:String>Employee</system:String>
                 <system:String>Contractor</system:String>
                 <system:String>Supplier</system:String>
              </CompositeCollection>
           </dg:DataGridComboBoxColumn.ItemsSource>
        </dg:DataGridComboBoxColumn>

in this the items displayed have a property called RoleProperty. you would also need an xnl namespace defintion at the top of your xaml (with the rest of them like:

   xmlns:system="clr-namespace:System;assembly=mscorlib"

to let you include the system namespace. (to get access to the Strings)

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • 3
    `` would be significantly more efficient than ``, and more readable too - most people don't know what a `CompositeCollection` is, let alone that it can be used this way. An array is a much cleaner choice in my opinion. – Ray Burns Jan 11 '10 at 23:04
  • excellent idea ray. i was coding too fast using intellisense and composite collection came up first. – Aran Mulholland Jan 11 '10 at 23:08
  • gave it a go (and in the two minutes or so i tried) and couldnt get array instantiated in xaml, said something about no public constructor – Aran Mulholland Jan 11 '10 at 23:10
3

You can just use a standard ComboBox with your static values as ComboBoxItems like so:

<ComboBox>
  <ComboBoxItem>Employee</ComboBoxItem>
  <ComboBoxItem>Contractor</ComboBoxItem>
  <ComboBoxItem>Supplier</ComboBoxItem>
</ComboBox>
Smixx
  • 345
  • 1
  • 9