无法获得双向绑定工作:)

时间:2012-09-22 14:00:41

标签: wpf data-binding two-way

我有一个项目,它设置了用于监视的动态选项卡,因此托管了几个“身份”对象,这些对象是INotifyPropertyChanged并在虚拟应用程序中单独测试。但是,我无法让绑定在我正在使用的应用程序中正常工作。它们的设置就是:

(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);

上面这个东西运行正常,大概是因为它是单向的。但这两个只能单向工作:

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

我注意到在调试我的保存例程时,上面控件中DataContext中引用的Identity对象确实包含更改的值,但是我在上面的foreach中使用的ObservableCollection中的那个不会更新。因此,不知何故,Identity对象将从最初用于在foreach期间设置DataContext属性的对象进行复制。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

右。我无法理解这一点,但到目前为止:

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

不起作用,但是:

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

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

完美无缺。我想这只是WPF的限制?