WPF Datagrid Combobox SelectedItem not Binding to Powershell Object correctly

孤者浪人 提交于 2019-12-25 09:29:46

问题


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.

Object being added as Itemssource


回答1:


You need to define DisplayMemberPath and SelectedItem binding. If you have provided some more code I could have shown you exactly what it should look like. Here is an example from some of my code...

    <ComboBox ItemsSource="{Binding Units}" DisplayMemberPath="Symbol" SelectedItem="{Binding SelectedToUnit}" />



回答2:


Here I created a more complete example using a template datagrid column.

            <DataGridTemplateColumn Header="Declared">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  SelectedValue="{Binding DummyColumn}"  DisplayMemberPath="Name" SelectedValuePath="Number" 
                                   ItemsSource="{Binding DataContext.DummyCollection, Source={x:Reference dummyElement}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

I need a reference to a dummy element to get data context right...

    <FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>

In the code behind here is my data class and collection

    public class DummyClass
    {
        public int Number { get; set; }
        public string Name { get; set; }
    }

...

        DummyClassCollection = new ObservableCollection<DummyClass>();

        DummyClassCollection.Add(new DummyClass() { Name = "Item1", Number = 0 });
        DummyClassCollection.Add(new DummyClass() { Name = "Item2", Number = 1 });
        DummyClassCollection.Add(new DummyClass() { Name = "Item3", Number = 2 });

The data table which to which the datagrid is bound...

        DataTable table = new DataTable();
        table.Columns.Add("DummyColumn", typeof(int));
        table.Columns.Add("Bool", typeof(bool));
        table.Rows.Add(1,true);
        table.Rows.Add(2,false);

So comparing this example against yours, it seems perhaps that the problem is SelectedValue versus SelectedItem.

Again is the very hard to sort out your problem without knowing more about the structure of your bound data.



来源:https://stackoverflow.com/questions/44093526/wpf-datagrid-combobox-selecteditem-not-binding-to-powershell-object-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!