【问题标题】:WPF Datagrid Combobox SelectedItem not Binding to Powershell Object correctlyWPF Datagrid Combobox SelectedItem 未正确绑定到 Powershell 对象
【发布时间】:2017-05-21 05:31:18
【问题描述】:

我在将我的 Datagrid ComboBox 选定项正确绑定到我的 PowerShell 对象时遇到了一些问题。我正在使用带有“UpdateSourceTrigger=PropertyChanged”参数的 ComboBox 的双向绑定。源对象正确地作为项目添加到组合框,并且源对象将随着选择的更改而更新。但是,当我保存对象或第一次启动时,所选项目不会被绑定。而是将所有 ComboBox 生成为没有选定值。

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 应用程序启动:

您可以看到,当应用程序启动时,ComboBox 项已正确绑定到 ItemsSource 对象的“ItemCount”字段。应该发生什么(或者至少我想要实现的)是所选项目应该是在 ItemsSource 对象的“位置”字段中定义的项目。

这是正在发生的事情的细分:

我不确定我做错了什么。任何帮助将不胜感激。

Object being added as Itemssource

【问题讨论】:

    标签: wpf powershell xaml binding datagrid


    【解决方案1】:

    您需要定义 DisplayMemberPath 和 SelectedItem 绑定。如果您提供了更多代码,我可以准确地向您展示它应该是什么样子。这是我的一些代码中的一个示例...

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

    【讨论】:

    • 您好,感谢您的回复。您能否详细说明“DisplayMemberPath”的设置是什么?上面我用一些 PS 代码编辑了我的原始帖子,概述了对象如何应用于 Datagrid。如果您需要更多代码,请告诉我。我很想解决这个问题!
    • 在我的示例中,Symbol 是 DataTable 中列的名称,在我的例子中是 ItemsSource。一切都取决于组合框的 ItemsSource 的组织。我怀疑在您的情况下,这只是一个整数列表,因此您可以将其省略。
    • 是的,我的 itemsource 是一个包含整数 1-10 的列。然后我有第二列包含一个整数,我想成为组合框的选定值。绑定的工作原理是,如果我从组合框中选择一个整数,我的 itemsource 对象会正确更新,但是一旦刷新数据网格,组合框就会返回不显示任何选定项目...
    • datagrid 中组合框的问题在于您有两个 ItemsSource,一个用于表格,一个用于组合框。我怀疑你混淆了这两者。 SelectedItem 是与数据网格的 ItemsSource 中的列的绑定。我认为这就是问题所在。但是你现在已经展示了使用任一 ItemsSource 的组织,所以我不知道。
    【解决方案2】:

    在这里,我使用模板数据网格列创建了一个更完整的示例。

                <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>
    

    我需要一个虚拟元素的引用来正确获取数据上下文...

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

    这里后面的代码是我的数据类和集合

        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 });
    

    datagrid绑定的数据表...

            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);
    

    因此,将此示例与您的示例进行比较,似乎问题在于 SelectedValue 与 SelectedItem。

    在不了解绑定数据结构的更多信息的情况下,很难解决您的问题。

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 2014-10-10
      • 2010-10-24
      • 2011-11-01
      • 2015-08-04
      • 2020-01-30
      • 2021-10-18
      • 2017-11-09
      相关资源
      最近更新 更多