【问题标题】:DatagridComboBoxColumn.ItemsSource does not reflect changes from source other than the source bound for datagridDatagridComboBoxColumn.ItemsSource 不反映来自数据网格绑定源以外的源的更改
【发布时间】:2011-11-30 01:35:07
【问题描述】:

ComboBox 项目不反映对其来源所做的更改

这是我想要完成的任务: 我有一个绑定到数据库表的 WPF 数据网格,在数据网格内有一个组合框(组 ID)列绑定到数据库表中的列之一;组合框 items 来自另一个表(组 ID 列表)。现在的问题是当分组ID列表从其他表更改时,组合框项目不生效。 任何人都可以帮忙吗?卡了很久。

这是 XAML 代码:

<DataGridTemplateColumn Header="Group ID">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding GroupID, Mode=OneWay}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox Name="ComboBoxTeamGrpID" SelectedItem="{Binding GroupID, Mode=TwoWay}" ItemsSource="{StaticResource ResourceKey=GroupIDList}">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

下面是 GroupIDList 的代码:

public class GroupIDList : List<string>
       {
           public GroupIDList()
        {
            try
            {

                string tmp = ConfigurationManager.AppSettings["DataSvcAddress"];
                Uri svcUri = new Uri(tmp);
                JP790DBEntities context = new JP790DBEntities(svcUri);

                var deviceQry = from o in context.Devices
                                where o.GroupID == true
                                select o;
                DataServiceCollection<Device> cList = new DataServiceCollection<Device>(deviceQry);

                for (int i = 0; i < cList.Count; i++)
                {
                    this.Add(cList[i].ExtensionID.Trim());
                }

                this.Add("None");

                //this.Add("1002");
                //this.Add("1111");
                //this.Add("2233");
                //this.Add("5544");
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }
    }

Here is another problem related, can anyone help? thank you.

【问题讨论】:

    标签: wpf binding datagrid combobox itemssource


    【解决方案1】:

    WPF 不会每次都轮询并检查您的列表是否已更改。为了做到这一点,正如雷切尔指出的那样,你应该做这样的事情:

    public class GroupIDList : ObseravableCollection<string>

    编辑:

    这是我的建议:

    我实际上不会像你那样做。我所做的是为整个网格创建一个视图模型,如下所示: public class MyGridViewModel : DependencyObject

    我将用作我的网格的数据上下文:

    DataContext = new MyGridViewModel ();
    

    现在 MyGridViewModel 的实现将包含一个代表我的 GridRows 的 ViewModel 列表,它是一个 ObservableCollection

    public ObservableCollection<RowGridViewModel> RowItemCollection { get; private set; }
    

    我会在我的 dataGrid 中这样:

     <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding RowItemCollection}" SelectionMode="Extended" SelectionUnit="Cell">
            <DataGrid.Columns>
    

    您需要做的就是用正确的数据填写您的 RowItemColleciton,然后将您的列绑定到 RowGridViewModel 中的正确属性...在您的情况下,它看起来像(但您必须初始化 GroupIDList :

    public class RowGridViewModel: DependencyObject
    {
    
         public List<String> GroudIDList { get; set;
          }
    
        }
    

    如果有帮助,请告诉我

    【讨论】:

    • 请参阅雷切尔的回复。有什么建议吗?谢谢。
    • @Jeremy 检查我的编辑,如果您需要更多详细信息,请告诉我。
    【解决方案2】:

    这要么是因为您的 GroupIdListList 而不是 ObservableCollection,要么是因为您绑定到了 StaticResource,WPF 假定它没有变化,因此只加载一次。

    将您的List&lt;string&gt; 更改为ObservableCollection&lt;string&gt;,它会在集合发生更改时自动通知用户界面,如果这仍然不起作用,请将您的ItemsSourceStaticResource 更改为RelativeSource 绑定,比如

    ItemsSource="{Binding 
        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, 
        Path=DataContext.GroupIdList}"
    

    编辑

    拥有 DataGrid 的 ItemsSource 集合的父 ViewModel 应如下所示。只需为GroupIdList 添加另一个公共属性并让它返回您的列表。然后使用上面的RelativeSource绑定来访问它,假设你的DataGrid的ItemsSource是以&lt;DataGrid ItemsSource="{Binding MyDataGridItemsSource}" ... /&gt;的形式绑定的

    public class MyViewModel
    {
        private ObservableCollection<MyDataObject> _myDataGridItemsSource;
        public ObservableCollection<MyDataObject> MyDataGridItemsSource
        {
            get { return _myDataGridItemsSource; }
            set 
            {
                if (value != _myDataGridItemsSource)
                {
                    _myObjects = value;
                    ReportPropertyChanged("MyDataGridItemsSource");
                }
            }
        }
    
        private ObservableCollection<string> _groupIdList = new GroupIdList();
        public ObservableCollection<string> GroupIdList
        {
    
            get { return _groupIdList; }
        }
    }
    

    【讨论】:

    • 更改为 ObservableCollection 和 RelativeSource 绑定,仍然没有运气,还有其他建议吗? 'ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DatagridTemplateColumn}}, Path=GroupIdList}" ' GroupIDList 在 Windows.Resources ' ... ' 谢谢。
    • 您的RelativeSource 绑定不正确。你指向DataGridTemplateColumn.GroupIdList,它不存在。在包含 DataGrid 数据的任何 ViewModel 上创建一个属性,让它返回您的ObservableCollection&lt;string&gt; GroupIdList,然后使您的RelativeSource 指向DataGrid,路径为DataContext.MyGroupIdListProperty,以便获取其数据的完整路径为DataGrid.DataContext.MyGroupIdListProperty
    • 我是通过wcf数据服务使用Entity Data Model来绑定数据的,如果创建一个新的类来匹配数据是要走的路,那么它如何绑定数据呢?对不起,如果问题很简单,我是 wpf 的新手。再次感谢您。
    • datagrid 中的 ComboBox 现在确实反映了更改,但是,对 datagrid 数据的更改不会被保存,有什么建议吗?我正在使用 context.SaveChanges()。
    • @Jeremy 我建议您发布另一个关于该问题的问题,并包含一些有关如何保存更改的示例代码。
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多