WPF数据绑定和列表项访问

时间:2014-03-31 17:56:54

标签: c# wpf listbox

我有一个按钮单击方法,用于填充列表,该列表依次提供ListBox控件。方法和WPF结果如下所示:

     private void checkForThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        CheckforThirdPartyUpdatesButton.IsEnabled = false;


        worker = new BackgroundWorker();

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            MainEntry.checkFor3PUpdates();

        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {

            if (comparisonStateView.Count == 0)
            {
                foreach (var item in RegScan_ThirdParty.comparisonListWithState)
                {
                    comparisonStateView.Add(item);
                }
            }

            ThirdPartyListBox.DataContext = comparisonStateView;

            CheckforThirdPartyUpdatesButton.IsEnabled = true;

        };

        worker.RunWorkerAsync();
    }

enter image description here

我试图添加检查已使用延期数量的代码并禁用"推迟"每个列表框项中符合该条件的按钮。这个调整后的代码执行我想要的逻辑,但它禁用了错误的按钮:

    private void checkForThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        CheckforThirdPartyUpdatesButton.IsEnabled = false;


        worker = new BackgroundWorker();

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            MainEntry.checkFor3PUpdates();

        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {

            if (comparisonStateView.Count == 0)
            {
                foreach (var item in RegScan_ThirdParty.comparisonListWithState)
                {
                    comparisonStateView.Add(item);
                }
            }

            ThirdPartyListBox.DataContext = comparisonStateView;

            CheckforThirdPartyUpdatesButton.IsEnabled = true;

            foreach (var rowItem in comparisonStateView)
            {
                if (rowItem.Item3.UsedDeferrals >= rowItem.Item2.MaxDefferals)
                {
                    Button ThirdPartyPostponeButton = sender as Button;
                    ThirdPartyPostponeButton.IsEnabled = false;
                }

            }

        };

        worker.RunWorkerAsync();
    }

结果:

enter image description here

有关如何达到预期效果的任何想法?我包含了一个最终的代码段,其中显示了"推迟"按钮被绑定和使用,以防它有所帮助:

   private void postponeThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {

        var context = ((FrameworkElement)sender).DataContext as Tuple<RegScan_ThirdParty.InstalledApplicationFromRegistryScan, RegScan_ThirdParty.ManifestRequiredApplication, RegScan_ThirdParty.RequiredApplicationState>;

        foreach (var rowItem in comparisonStateView)
        {
            if (rowItem.Item1.Name == context.Item1.Name)
            {
                comparisonStateView.Remove(rowItem);
                break;
            }

        }

        Button ThirdPartyPostponeButton = sender as Button;
        ThirdPartyPostponeButton.IsEnabled = false;

        if (context != null)
        {
            RegScan_ThirdParty.registryApplicationPostponeWorkflow(context);
        }

        ThirdPartyPostponeButton.IsEnabled = true;
    }

0 个答案:

没有答案