11 Replies - 425 Views - Last Post: 14 May 2014 - 03:41 AM Rate Topic: -----

#1 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 143
  • Joined: 17-October 11

General Data Binding questions

Posted 13 May 2014 - 05:14 AM

Hi,

I have a few general data binding (Using C# and WPF) questions I would like to ask:

1) If for example, I'm checking for a condition and it returns false or true, how would I prevent that data from being bound to my user control (e.g. datagrid?). That is, how do I clear or prevent a list that I'm sending over to be bound to my datagrid if a boolean is false for instance?

2)I noticed that if I bind several user controls to the same property and if I call a method is supposed to populate one control, all of the controls that bind that property also populates. Is there an easy fix for this other than to create new properties to bind for each user control?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: General Data Binding questions

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 05:36 AM

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

2)I noticed that if I bind several user controls to the same property and if I call a method is supposed to populate one control, all of the controls that bind that property also populates. Is there an easy fix for this other than to create new properties to bind for each user control?

Use read-only controls or one way binding. If you intend them to be read-write and two way bound controls, why don't you want the other controls to pick up the changes? Perhaps there's a problem with the way you decided to model your problem.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 05:44 AM

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

1) If for example, I'm checking for a condition and it returns false or true, how would I prevent that data from being bound to my user control (e.g. datagrid?). That is, how do I clear or prevent a list that I'm sending over to be bound to my datagrid if a boolean is false for instance?


What's wrong with just not sending it over?

public ObservableCollection<People> DeadPeople
{
    get
    {
        if (IsHaleyJoelOsment)
            return new ObservableCollection<People>(_deadPeople);
        return new ObservableCollection<People>();
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 143
  • Joined: 17-October 11

Re: General Data Binding questions

Posted 13 May 2014 - 07:30 AM

View PostSkydiver, on 13 May 2014 - 07:44 AM, said:

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

1) If for example, I'm checking for a condition and it returns false or true, how would I prevent that data from being bound to my user control (e.g. datagrid?). That is, how do I clear or prevent a list that I'm sending over to be bound to my datagrid if a boolean is false for instance?


What's wrong with just not sending it over?

public ObservableCollection<People> DeadPeople
{
    get
    {
        if (IsHaleyJoelOsment)
            return new ObservableCollection<People>(_deadPeople);
        return new ObservableCollection<People>();
    }
}



The problem is then I would be returning a value in a void function. E.g. I have a function that checks if a lot number is inside the db. If it's inside the db, do not bind the data/prevent data from being bound. Just display error message instead.

Here's what I have so far:

public void ReadLot(LotInformation lot)
        {
            try
            {
                using (var db = new DDataContext())
                {

                    // Check for duplicates
                    bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lot.lot_number);

                    if (isDuplicate == true)
                    {
                        // Inform user that the lot_number already exists
                        errorwindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                        dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                        logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                        return;
                    }
                    
                    Console.WriteLine("does not exist. yay");

} 
}
}


The problem is the message pops up but the data still gets bound. What should I Do in a case like above?

I tried using Clear() (e.g. FamiliesList.Clear(); ) on my lists that are being bound to the datagrid but it doesn't work for some reason.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,783
  • Joined: 08-June 10

Re: General Data Binding questions

Posted 13 May 2014 - 08:15 AM

What data gets bound? What object in your example is bound to a UI component? If you're trying to pre-check something, save it to a local variable instead of your bound property, do your test, and if it's valid, set the bound property to the temp variable.
Was This Post Helpful? 0
  • +
  • -

#6 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 143
  • Joined: 17-October 11

Re: General Data Binding questions

Posted 13 May 2014 - 11:27 AM

View PostSkydiver, on 13 May 2014 - 07:36 AM, said:

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

2)I noticed that if I bind several user controls to the same property and if I call a method is supposed to populate one control, all of the controls that bind that property also populates. Is there an easy fix for this other than to create new properties to bind for each user control?

Use read-only controls or one way binding. If you intend them to be read-write and two way bound controls, why don't you want the other controls to pick up the changes? Perhaps there's a problem with the way you decided to model your problem.


Yup, in my XAML, I set my datagrid to readonly and mode=OneWay binding. However, since the same property bound to two datagrids is on both tabs in one view, both datagrids on each tab get updated when one of the properties updates. So, I want one datagrid to be clear and the datagrid on the other tab to be filled with data.

This post has been edited by Eowyn27: 13 May 2014 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 07:51 PM

So bind the two to different instances of the same class. One class is filled and another class is empty. I'm not seeing the difficulty here.

View PostEowyn27, on 13 May 2014 - 10:30 AM, said:

View PostSkydiver, on 13 May 2014 - 07:44 AM, said:

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

1) If for example, I'm checking for a condition and it returns false or true, how would I prevent that data from being bound to my user control (e.g. datagrid?). That is, how do I clear or prevent a list that I'm sending over to be bound to my datagrid if a boolean is false for instance?


What's wrong with just not sending it over?

public ObservableCollection<People> DeadPeople
{
    get
    {
        if (IsHaleyJoelOsment)
            return new ObservableCollection<People>(_deadPeople);
        return new ObservableCollection<People>();
    }
}



The problem is then I would be returning a value in a void function. E.g. I have a function that checks if a lot number is inside the db. If it's inside the db, do not bind the data/prevent data from being bound. Just display error message instead.

Here's what I have so far:

public void ReadLot(LotInformation lot)
        {
            try
            {
                using (var db = new DDataContext())
                {

                    // Check for duplicates
                    bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lot.lot_number);

                    if (isDuplicate == true)
                    {
                        // Inform user that the lot_number already exists
                        errorwindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                        dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                        logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                        return;
                    }
                    
                    Console.WriteLine("does not exist. yay");

} 
}
}


The problem is the message pops up but the data still gets bound. What should I Do in a case like above?

I tried using Clear() (e.g. FamiliesList.Clear(); ) on my lists that are being bound to the datagrid but it doesn't work for some reason.

Which line above does the binding? As far as I can see, nothing there does. So that means that you have to tell the code that does do the binding to not do so.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 08:00 PM

You had me really puzzled about your note about trying to call FamiliesList.Clear(). I could'nt figure out what you were talking about. Apparently you were referring back to your other question in another subforum. This is part of the reason why we highly encourage people to just keep building up on their old topic when asking questions that are related to it. Starting a brand new topic, but assuming that readers of the new topic have read your other topic is a recipe for losing audience interest. Remember that this board is inhabited by volunteers who have jobs and lives outside of this forum. The harder it is you make it for us to help you, less inclined we will be to help and just move on to help somebody else.
Was This Post Helpful? 0
  • +
  • -

#9 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 143
  • Joined: 17-October 11

Re: General Data Binding questions

Posted 13 May 2014 - 08:00 PM

View PostSkydiver, on 13 May 2014 - 09:51 PM, said:

So bind the two to different instances of the same class. One class is filled and another class is empty. I'm not seeing the difficulty here.

View PostEowyn27, on 13 May 2014 - 10:30 AM, said:

View PostSkydiver, on 13 May 2014 - 07:44 AM, said:

View PostEowyn27, on 13 May 2014 - 08:14 AM, said:

1) If for example, I'm checking for a condition and it returns false or true, how would I prevent that data from being bound to my user control (e.g. datagrid?). That is, how do I clear or prevent a list that I'm sending over to be bound to my datagrid if a boolean is false for instance?


What's wrong with just not sending it over?

public ObservableCollection<People> DeadPeople
{
    get
    {
        if (IsHaleyJoelOsment)
            return new ObservableCollection<People>(_deadPeople);
        return new ObservableCollection<People>();
    }
}



The problem is then I would be returning a value in a void function. E.g. I have a function that checks if a lot number is inside the db. If it's inside the db, do not bind the data/prevent data from being bound. Just display error message instead.

Here's what I have so far:

public void ReadLot(LotInformation lot)
        {
            try
            {
                using (var db = new DDataContext())
                {

                    // Check for duplicates
                    bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lot.lot_number);

                    if (isDuplicate == true)
                    {
                        // Inform user that the lot_number already exists
                        errorwindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                        dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                        logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                        return;
                    }
                    
                    Console.WriteLine("does not exist. yay");

} 
}
}


The problem is the message pops up but the data still gets bound. What should I Do in a case like above?

I tried using Clear() (e.g. FamiliesList.Clear(); ) on my lists that are being bound to the datagrid but it doesn't work for some reason.

Which line above does the binding? As far as I can see, nothing there does. So that means that you have to tell the code that does do the binding to not do so.


What do you mean? Do you mean I have to do something like DataGrid.ItemsSource = null? The problem is the name of my custom datagrid is not being recognized in my viewmodel, only in my view.

Also, I have one more thing. I think this might be what you meant:

public string LotNumber
        {
            get
            {
                return lotNumber;
            }
            set
            {
                using (var db = new DDataContext())
                {
                    lotNumber = value;
                    // Check for duplicates
                    bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lotNumber);

                    if (isDuplicate == true)
                    {
                        ComponentsList = null;
                        FamiliesList = null;
                        ExpirationDate = null; 
                        LotNumber = null;
                        lotNumber = null;
                        // Inform user that the lot_number already exists
                        errorwindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                        dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                        logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                       return;
                    }
                    else
                    {

                        lotNumber = value;
                        
                    }
                    RaisePropertyChanged("LotNumber");
                }
            }
        }


Problem is, the above code loops back. How can I just clear/prevent binding if the bool is true as shown above and not have it loop?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 08:13 PM

View PostEowyn27, on 13 May 2014 - 11:00 PM, said:

View PostSkydiver, on 13 May 2014 - 09:51 PM, said:

So bind the two to different instances of the same class. One class is filled and another class is empty. I'm not seeing the difficulty here.


What do you mean? Do you mean I have to do something like DataGrid.ItemsSource = null? The problem is the name of my custom datagrid is not being recognized in my viewmodel, only in my view.


Assuming View1.DataGrid is the one you want to be empty and View2.DataGrid is the one you want to be filled, you would have the something like:
class StuffDataModel
{
    ObservableCollection<Stuff> _stuffList;

    public StuffDataModel()
    {
        _stuffList = new ObservableCollection<Stuff>();
    }

    public StuffDataModel(IRepository<Stuff> stuffRepsitory)
    {
        _stuffList = new ObservableCollection<Stuff>(stuffRepository.SelectAll());
    }

    ObservableCollection<Stuff> StuffList
    {
        get { return _stuffList; }
    }
}

:

View1.DataContext = new StuffDataModel();
View2.DataContext = new StuffDataModel(db.StuffRepository);



Notice that both views take a StuffDataModel, but for one you provide an empty instance, and for the other you provide an instance that has data loaded into it.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: General Data Binding questions

Posted 13 May 2014 - 08:36 PM

View PostEowyn27, on 13 May 2014 - 11:00 PM, said:

Also, I have one more thing. I think this might be what you meant:
Spoiler

Problem is, the above code loops back. How can I just clear/prevent binding if the bool is true as shown above and not have it loop?

Your line 20 is causing a recursive call.
Was This Post Helpful? 0
  • +
  • -

#12 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 143
  • Joined: 17-October 11

Re: General Data Binding questions

Posted 14 May 2014 - 03:41 AM

View PostSkydiver, on 13 May 2014 - 10:36 PM, said:

View PostEowyn27, on 13 May 2014 - 11:00 PM, said:

Also, I have one more thing. I think this might be what you meant:
Spoiler

Problem is, the above code loops back. How can I just clear/prevent binding if the bool is true as shown above and not have it loop?

Your line 20 is causing a recursive call.


Yup, thanks. I removed it and I realized that another method bound the values without proper validation first. So, I added validation to that method.

Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1