简体   繁体   中英

DataBinding DataGrid to ObservableCollection<T> is creating 2 different instances of the DataGrid and only the default is shown

The data binding works as it I intend, kind of... The real issue I'm running into now is what I believe to be 2 different instances of my User Control, but only the original, debug list I implemented is showing.

In short, I am building 2 lists that are technically bound to the data grid, the default debugging list I created in the default constructor and then the real list I created to bind to the data grid.

Every time I click on the user control with the data grid, the default constructor adds another line to my debugging list and displays it on the screen.

Every time I click the button that builds a list of selected options on a separate user control I can see my the options add on to the list of options I had been creating and technically set it to the data context of the data grid, the same way the default debug list does, except when I click back over to the data grid user control, the default constructor runs again, ads another line to my debug list, and displays the debug list that is being built.

Here's a copy of the class with a couple lines I added to help debug the problem.

 public partial class QuotePreview : UserControl
{
    private SelectionList _selectionList;
    private SelectionList temp;

    public QuotePreview()
    {
        InitializeComponent();
        _selectionList = (SelectionList)this.DataContext;
    }

    private void QuotePreview_Loaded(object sender, RoutedEventArgs e)
    {

        //Adds item to Debugging list
        _selectionList.SelectedOptions.Add(
            new Selection
            {
                ModelNumber = "this",
                Description = "really",
                Price = "sucks"
            });

    }

    public QuotePreview(SelectionList selectedOptions)
    {

        InitializeComponent();
        _selectionList = (SelectionList)this.DataContext;

        temp = selectedOptions;

        //The list I am actually trying to display
        _selectionList.AddRange(selectedOptions);

        QuotePreview_Loaded();
    }

    private void QuotePreview_Loaded()
    {
        foreach (var options in temp.SelectedOptions)
        {
            _selectionList.SelectedOptions.Add(options);
        }

        QuotePreviewDataGrid.ItemsSource = _selectionList.SelectedOptions;
    }
}

The implementation of the default constructor, is called every time the user control / tab, is clicked on. When that happens, _selectionList is set to the data context of the user control, followed by the Loaded Event which adds a line to my data grid.

In another user control where I select the options I want to add to my data grid user control, I click a button that creates a list of the options I want to be added and calls the custom constructor I wrote. Once the constructor finishes, it calls a custom Loaded Event method that I created for shits and giggles, that adds the selected options to my _selectionList.

Now once I click on the data grid user control again, it goes through the whole default process, and adds another default line.

If I go back a tab and say I want these options again and go back to the data grid, it again goes through the default process and adds another default line.

Whats most intriguing though is that I can see both of the selectionLists build since I dont clear the in between processes. I see a list build of the options i want to display and a list build of the default options build...

Oh, also, SelectionList does implement ObservableCollection

i don't follow exactly what you are asking but loaded event will fire whenever load is required and in your case you are switching between the views , TabControl will not render its content until it is required !

 bool _isDefaultItemAdded = false
private void QuotePreview_Loaded(object sender, RoutedEventArgs e)
{
    if(!_isDefaultItemAdded)
    {
       //Adds item to Debugging list
         _selectionList.SelectedOptions.Add(
        new Selection
        {
            ModelNumber = "this",
            Description = "really",
            Price = "sucks"
        });
        _isDefaultItemAdded = true;
    }


}

I finally came up with a solution to the problem.

public static class QuotePreview
{
    public static ObservableCollection<PurchasableItem> LineItems { get; private set; }

    static QuotePreview()
    {
        LineItems = new ObservableCollection<PurchasableItem>();
    }

    public static void Add(List<PurchasableItems> selections)
    {
        foreach (var selection in selections)
        {
            LineItems.Add(selection);
        }
    }

    public static void Clear()
    {
        LineItems.Clear();
    }
}

public class QuoteTab : TabItem
{
    public ObservableCollection<PurchasableItem> PreviewItems { get; private set; }

    public QuoteTab()
    {          
        Initialize()

        PreviewItems = QuotePreview.LineItems;

        DataGrid.ItemSource = PreviewItems
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM