I am having a bit of trouble correctly binding my Datagrid ComboBox selected item to my PowerShell object. I am using a two way binding for the ComboBox with an 'UpdateSourceTrigger=PropertyChanged' parameter. The source objects correctly get added as items to the ComboBoxes and the source object will update as a selection is changed. However When I save the object or else launch for the first time, the selected items do not get bound. Instead all ComboBoxes are generated as having no selected value.
XAML:
<DataGrid Name="CustomDescription_Fields_DG" HorizontalAlignment="Left" Width="626" Margin="185,113,0,87" IsReadOnly="True" AutoGenerateColumns="False" GridLinesVisibility="None" AlternatingRowBackground="#FFEFEFF2" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Field}" Header="Applied Fields" Width="395"/>
<DataGridTemplateColumn Header="Position" Visibility="Visible" Width="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Path=Position, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding ItemCount}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground"
Value="{DynamicResource
{x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
WPF application launched:
You can see that when the application is launched that the ComboBox items are correctly bound to the 'ItemCount' field of the ItemsSource object. What should happen (or at least what I'm trying to achieve) is that the selected item should be the item that is defined within the 'Position' field of the ItemsSource object.
This is a breakdown of what is happening:
I'm not to sure what I am doing wrong. Any help would be very much appreciated.
