C# WPF数据绑定和列表项访问

C# WPF数据绑定和列表项访问,c#,wpf,listbox,C#,Wpf,Listbox,我有一个按钮点击方法,它填充一个列表,而这个列表又提供一个ListBox控件。方法和WPF结果如下所示: private void checkForThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e) { CheckforThirdPartyUpdatesButton.IsEnabled = false; worker = new BackgroundWorker();

我有一个按钮点击方法,它填充一个列表,而这个列表又提供一个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();
    }

我正在尝试添加代码,以检查已使用的延迟数,并在符合该条件的每个列表框项目中禁用“延迟”按钮。此调整后的代码执行我想要的逻辑,但它禁用了错误的按钮:

    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();
    }
结果:

关于如何达到预期效果,有什么想法吗?我将提供最后一段代码,说明如何绑定和使用“推迟”按钮,以防它有所帮助:

   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;
    }
private void postponethirdpartyUpdates按钮单击(对象发送者,路由目标)
{
var context=((FrameworkElement)sender.DataContext作为元组;
foreach(comparisonStateView中的变量rowItem)
{
if(rowItem.Item1.Name==context.Item1.Name)
{
comparisonStateView.Remove(行项目);
打破
}
}
按钮第三方PostPoneButton=发送方为按钮;
第三方PostPoneButton.IsEnabled=false;
if(上下文!=null)
{
RegScan_ThirdParty.registryApplicationsUpposeWorkflow(上下文);
}
第三方PostPoneButton.IsEnabled=true;
}

您是说应该禁用推迟按钮而不是第三方推迟按钮吗?是的,这是正确的。我可以看到您只是将
IsEnabled
设置为
第三方推迟按钮
?您禁用推迟按钮的代码在哪里?它位于第二个发布的代码块中:
ThirdPartyPostponeButton.IsEnabled=false我想看看你的观点