In WPF the solution is a little bit more complex, however with a bit of patience you'll find it's wonderful. Here is an example on how to use it...
XAML:
- <Window x:Class="WpfApplication1.MainWindow"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
xmlns:core="clr-namespace:System;assembly=mscorlib"
-
xmlns:local="clr-namespace:WpfApplication1"
-
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
-
-
<Window.Resources>
-
<ObjectDataProvider x:Key="RecordValues" MethodName="GetValues"
-
ObjectType="{x:Type core:Enum}">
-
<ObjectDataProvider.MethodParameters>
-
<x:Type Type="local:RecordValues"/>
-
</ObjectDataProvider.MethodParameters>
-
</ObjectDataProvider>
-
</Window.Resources>
-
-
-
<Grid>
-
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}"
-
Margin="10" Name="dataGrid1">
-
<DataGrid.Columns>
-
<DataGridComboBoxColumn Header="Combo" Width="300"
-
SelectedItemBinding="{Binding Values}"
-
ItemsSource="{Binding Source={StaticResource RecordValues}}" />
-
</DataGrid.Columns>
-
</DataGrid>
-
</Grid>
-
</Window>
-
Notice that there are a few things here that need attention, first off, a ObjectDataProvider is requried in your XAML to be able to treat an enum in your project as visible to the combo column. There are one or two namespaces here that you will need to use also, such as xmlns:core and xmlns:local.
Once that is done, the rest is fairly simple, notice i created a DataGridComboBoxColumn in the XAML, and have used the binding properties
SelectedItemBinding which should be a binding to the data that is to be displayed and changed, and
ItemsSource which is a reference to the enum we adapted with the ObjectDataProvider.
Next is the code...
C#:
-
public partial class MainWindow : Window
-
{
-
ObservableCollection<Record> Records = new ObservableCollection<Record>();
-
-
public MainWindow()
-
{
-
InitializeComponent();
-
}
-
-
private void Window_Loaded(object sender, RoutedEventArgs e)
-
{
-
Records.Add(new Record());
-
Records.Add(new Record());
-
dataGrid1.DataContext = Records;
-
}
-
}
-
-
public enum RecordValues
-
{
-
Apple,
-
Banana,
-
Coconut,
-
Dambul
-
}
-
-
public class Record
-
{
-
private RecordValues _value = RecordValues.Apple;
-
public RecordValues value
-
{
-
get { return _value; } set { _value = value; }
-
}
-
}
-
For the purposes of this answer, it's not sensible to include data retrieved from a database, instead i've opted for a custom class, it's important to note that you must use properties, just having a public variable inside a class is not good enough, and it's just plain good practice. Here you can also see the little enum i created, this can however be collection of strings if you wished.
Lastly, remember to use the
DataContext property to fill your grid rather than
ItemsSource.
Hope this helps!
Aimee Bailey.