0

我有一个项目,它设置了用于监视的动态选项卡,因此托管了几个“身份”对象,这些对象是 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 属性的对象中复制而来的。我在这里做错了什么?

4

1 回答 1

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 的一个限制?

于 2012-09-23T00:33:38.557 回答