0

I have a project which sets up dynamic tabs for monitoring and thus hosts several "identity" objects which are INotifyPropertyChanged and work individually as tested in a dummy app. However, I can't get the bindings to work correctly in the app I'm working on. They are set up i.e.:

(Configuration.Identities is an ObservableCollection<Identity>)
foreach (Identity id in Configuration.Identities)
{

   workTab = new TabItem();
   workSettingsTab = new SettingsTabItem();

   workTab.DataContext = id;
   Binding workTabHeaderBinding = new Binding("Username");
   workTabHeaderBinding.Converter = new ToLowerConverter();
   workTab.SetBinding(TabItem.HeaderProperty, workTabHeaderBinding);

   workSettingsTab.DataContext = id;
   Binding workSettingsTabHeaderBinding = new Binding("Username");
   workSettingsTabHeaderBinding.Converter = new ToUpperConverter();
   workSettingsTab.SetBinding(TabItem.HeaderProperty, workSettingsTabHeaderBinding);

This stuff above works fine, presumably because it's one-way. But these two only work one-way:

workSettingsTab.txtUsername.DataContext = id;
Binding usernameBinding = new Binding("Username");
usernameBinding.Mode = BindingMode.TwoWay;
workSettingsTab.txtUsername.SetBinding(TextBox.TextProperty, usernameBinding);

Binding getJournals = new Binding("LoadJournals");
scrapeJournals.Mode = BindingMode.TwoWay;
workSettingsTab.chkScrapeJournals.DataContext = id;

workSettingsTab.chkScrapeJournals.SetBinding(CheckBox.IsCheckedProperty, scrapeJournals);

I noticed when debugging my save-routine, that the Identity object referenced in DataContext in above controls does indeed contain the changed values, but the one in the ObservableCollection I used in the foreach above is not updated. So somehow the Identity object gets copied from the one initially used to set the DataContext property during the foreach. What am I doing wrong here?

4
  • 2
    You should take a serious look at data templating, all of that code is unnecessary.
    – brunnerh
    Sep 22, 2012 at 14:17
  • And you're raising PropertyChanged("UserName") in the setter of the UserName property?
    – McGarnagle
    Sep 22, 2012 at 22:11
  • Yes, it fires off the NotifyPropertyChanged("Username");. The strange part is that the object inside the DataContext property changes but for some reason the original doesn't. I also bind to more than one object which is why I don't template. Would it be best to consolidate the properties into a single wrapper / viewmodel class and template instead? :P
    – Daniel
    Sep 22, 2012 at 23:43
  • This is so odd. I made a dummy project with the same setup. Using the code above it does NOT work, it still updates only the item inside the DataContext property, but using templating it works all the way. The heck o.O
    – Daniel
    Sep 23, 2012 at 0:13

1 Answer 1

0

Right. I can't get this to make sense, but so far:

        ObservableCollection<SwitchClass> col2 = new ObservableCollection<SwitchClass>();
        col2.Add(new SwitchClass{theSwitch=false});
        col2.Add(new SwitchClass{theSwitch=true});
        col2.Add(new SwitchClass{theSwitch=false});
        col2.Add(new SwitchClass{theSwitch=true});

        customTab cTab;
        Int32 z = 0;
        Binding b;
        foreach (SwitchClass s in col2)
        {
            cTab = new customTab();
            cTab.theCheckbox.DataContext = s;
            b = new Binding("theSwitch");
            b.Mode = BindingMode.TwoWay;
            cTab.SetBinding(CheckBox.IsCheckedProperty, b);
            cTab.Header = "Tab " + z.ToString();
            z++;
            tabControl.Items.Add(cTab);
        }

Does not work, but:

        ObservableCollection<SwitchClass> col2 = new ObservableCollection<SwitchClass>();
        col2.Add(new SwitchClass{theSwitch=false});
        col2.Add(new SwitchClass{theSwitch=true});
        col2.Add(new SwitchClass{theSwitch=false});
        col2.Add(new SwitchClass{theSwitch=true});

        customTab cTab;
        Int32 z = 0;
        foreach (SwitchClass s in col2)
        {
            cTab = new customTab();
            cTab.DataContext = s;
            cTab.Header = "Tab " + z.ToString();
            z++;
            tabControl.Items.Add(cTab);
        }

with

<CheckBox Name="theCheckbox" Width="200" Height="50" Content="Checkbox" IsChecked="{Binding theSwitch}" />

works perfectly. I suppose this is just a limitation of WPF?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.