开发者

DatagridComboBoxColumn.ItemsSource does not reflect changes from source other than the source bound for datagrid

开发者 https://www.devze.com 2023-04-10 14:16 出处:网络
ComboBox items do not reflect changes made from its source Here is what I am trying to accomplish: I have a WPF datagrid that binding to a database table, inside the datagrid there is a combobox(grou

ComboBox items do not reflect changes made from its source

Here is what I am trying to accomplish: I have a WPF datagrid that binding to a database table, inside the datagrid there is a combobox(group ID) column bind to one of the columns from the database table; the combobox items are from another table(a list of group ID). The problem now is when the groupd ID list is changed from other table, the combo box items does not take effect. Can anyone help? Have been stuct for a long time.

Here is XAML code:

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

Here is the code for 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 &l开发者_运维百科t; 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.


It is either because your GroupIdList is a List and not an ObservableCollection, or because you're binding to a StaticResource, which WPF assumes is unchanged so is only loaded once.

Change your List<string> to an ObservableCollection<string> which will automatically notify the UI when it's collection gets changed, and if that still doesn't work than change your ItemsSource from a StaticResource to a RelativeSource binding, such as

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

Edit

Your parent ViewModel which has your DataGrid's ItemsSource collection should look something like below. Simply add another public property for GroupIdList and have it return your list. Then use the above RelativeSource binding to access it, assuming your DataGrid's ItemsSource is bound in the form of <DataGrid ItemsSource="{Binding MyDataGridItemsSource}" ... />

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


WPF will not poll everytime and check if your list changed. In Order to do this, as Rachel pointed at you should do something like :

public class GroupIDList : ObseravableCollection<string>

EDIT :

Here is my suggestion :

I actually wouldn't do it the way you did. What I do is I create a View Model for the whole grid, that looks like : public class MyGridViewModel : DependencyObject

Which I would use as data context for my grid:

DataContext = new MyGridViewModel ();

Now the implementation of MyGridViewModel will contain a list of ViewModel that represent my GridRows, which is an ObservableCollection

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

I will this in my dataGrid as such :

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

and All you need to do, is to fill in you RowItemColleciton with the correct data, and then bind you Columns to the correct Property in RowGridViewModel...in your case it would look like (but you have to initialize the GroupIDList :

public class RowGridViewModel: DependencyObject
{

     public List<String> GroudIDList { get; set;
      }

    }

Let me if that help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号