Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF共享数据上下文/绑定_C#_Wpf_Xaml - Fatal编程技术网

C# WPF共享数据上下文/绑定

C# WPF共享数据上下文/绑定,c#,wpf,xaml,C#,Wpf,Xaml,我目前在WPF中的绑定有问题。我有一个两个标签的应用程序。第一个选项卡包含由列表填充的datagrid控件。第二个选项卡包含由列表填充的listbox控件。问题是,如果我在第一个选项卡上运行检查,它返回数据网格中的5行,那么第二个选项卡将填充5个虚拟列表框项。因此,这些控件显然共享了某些上下文,但我不确定如何纠正这一点 显示绑定的第一个选项卡的XAML: <DataGrid Name="WinUpdateDataGrid" Margin="10,79,0,114" ItemsSource=

我目前在WPF中的绑定有问题。我有一个两个标签的应用程序。第一个选项卡包含由列表填充的datagrid控件。第二个选项卡包含由列表填充的listbox控件。问题是,如果我在第一个选项卡上运行检查,它返回数据网格中的5行,那么第二个选项卡将填充5个虚拟列表框项。因此,这些控件显然共享了某些上下文,但我不确定如何纠正这一点

显示绑定的第一个选项卡的XAML:

<DataGrid Name="WinUpdateDataGrid" Margin="10,79,0,114" ItemsSource="{Binding}" CanUserAddRows="false" AutoGenerateColumns="true" HorizontalAlignment="Left" VerticalAlignment="Center" ColumnWidth="*" Height="161" Width="509" MaxHeight="150" MaxWidth="700"></DataGrid>
第二个选项卡的XAML:

<ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="C:\Users\Test\Desktop\Project\ACME-WPF\ACME-WPF\window-new-3.ico" Margin="5" Width="50"/>
                                <Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
                                <Button Name="ThirdPartyPostoneButton" Content="Postpone" Margin="5,5,0,0" Height="25"></Button>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.Name}" Margin="12,25,0,0"/>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.RequiredVersion}" Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.CustomUIMessage}" Margin="10,25,0,0" TextWrapping="Wrap" Foreground="Red"/>
                                <TextBlock Text="You have used " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item3.UsedDeferrals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" of " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.MaxDefferals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" deferrals for this update." Margin="3,25,0,0"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
 private void CheckforThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        CheckforThirdPartyUpdatesButton.IsEnabled = false;

        worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;

        DataContext = RegScan_ThirdParty.comparisonListWithState;

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

        worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
        {

        };

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


            CheckforThirdPartyUpdatesButton.IsEnabled = true;
        };

        worker.RunWorkerAsync();
    }
完整的XAML请求:

<Window x:Class="ACME_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ACME" Height="411.085" MaxWidth="555.668"
    SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"
    WindowStyle="ToolWindow"
    >
<Grid>
    <TabControl>
        <TabItem Header="Windows Updates">
            <Grid>
                <Image Name="WindowsUpdateDefaultImage" Source="C:\Users\Test\Desktop\Project\ACME-WPF\ACME-WPF\windowsupdate.png" Margin="0,16,488,286" Width="50"/>
                <TextBlock Name="WinUpdateStatusText" Width="Auto" HorizontalAlignment="Left" Margin="267,22,0,0" TextWrapping="Wrap" Text="{Binding Path=WindowsUpdateCompliance, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" FontSize="14" Foreground="DarkSlateBlue"/>
                <Button Name="CheckforWindowsUpdates" HorizontalAlignment="Left" Margin="54,18,0,0" VerticalAlignment="Top" Width="207" Content="Check for Windows Security Updates" Height="25" Click="CheckforWindowsUpdates_Click"/>
                <ProgressBar Name="WinUpdateProgressBar" HorizontalAlignment="Left" Height="18" Margin="54,52,0,0" VerticalAlignment="Top" Width="207"/>
                <DataGrid Name="WinUpdateDataGrid" Margin="10,79,0,114" ItemsSource="{Binding}" CanUserAddRows="false" AutoGenerateColumns="true" HorizontalAlignment="Left" VerticalAlignment="Center" ColumnWidth="*" Height="161" Width="509" MaxHeight="150" MaxWidth="700"></DataGrid>
                <Button Name="DownloadandInstallWinUpdatesButton" Content="Download and Install Windows Updates" HorizontalAlignment="Left" Margin="10,250,0,0" VerticalAlignment="Top" Width="227" Height="26" Click="DownloadandInstallWinUpdatesButton_Click"/>
                <ProgressBar Name="DownloadandInstallWinUpdatesButtonProgressBar" HorizontalAlignment="Left" Height="26" Margin="10,286,0,0" VerticalAlignment="Top" Width="227"/>
                <TextBlock Name="WinUpdateInstallationText" HorizontalAlignment="Left" Margin="256,250,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="Auto"/>
            </Grid>
        </TabItem>
        <TabItem Header="Third Party Updates">
            <Grid>
                <Button Name="CheckforThirdPartyUpdatesButton" Content="Check for Third Party Updates" Margin="10,11,339,304" Click="CheckforThirdPartyUpdatesButton_Click" MaxWidth="200"/>
                <ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="C:\Users\Test\Desktop\Project\ACME-WPF\ACME-WPF\window-new-3.ico" Margin="5" Width="50"/>
                                <Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" CommandParameter="{Binding Path=.}" Command="{Binding ElementName=ThirdPartyListBox, Path=DataContext.InstallComponentCommand}" Margin="5,5,0,0" Height="25"></Button>
                                <Button Name="ThirdPartyPostoneButton" Content="Postpone" Margin="5,5,0,0" Height="25"></Button>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.Name}" Margin="12,25,0,0"/>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.RequiredVersion}" Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.CustomUIMessage}" Margin="10,25,0,0" TextWrapping="Wrap" Foreground="Red"/>
                                <TextBlock Text="You have used " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item3.UsedDeferrals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" of " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.MaxDefferals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" deferrals for this update." Margin="3,25,0,0"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </TabItem>
        <TabItem Header="Information">
            <Grid>
                <Label Name="Info_ComputerNameLabel" Content="Computer Name: " Margin="10,13,397,253" RenderTransformOrigin="0.49,1"/>
                <Label Name="Info_UserNameLabel" Content="User Name: " Width="100" Margin="10,54,419,222"/>
                <Label Name="Info_IPAddressLabel" Content="IP Address: " Width="100" Margin="10,96,419,180"/>
                <TextBlock Name="Info_ComputerNameText" HorizontalAlignment="Left" Margin="137,18,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
                <TextBlock Name="Info_UserNameText" HorizontalAlignment="Left" Margin="137,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
                <TextBlock Name="Info_IPAddressText" HorizontalAlignment="Left" Margin="137,101,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

简短的回答:
{Binding}
不是“绑定到自身”(在某种意义上)的快捷方式。相反,
{Binding}
{Binding Path=.}
,它绑定到当前源


详细说明:绑定有一个源和一个路径。例如,您可以使用

<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}" />

但是,这会将源设置为控件本身,因此它将尝试访问UI控件的属性
x
(而不是当前数据上下文的属性
x
)。从我对你问题的理解来看,这不是你想要的;特别是,它不是
{Binding}
所做的:
{Binding}
保持源的原样(通常是某个父元素的
DataContext
),并绑定到源本身(相当于
路径=。

两个控件的当前源可能正在共享。我只想显式地设置
绑定
数据源,而不是执行快捷方式方法

更新
它实际上是在共享数据上下文。您可以为那些
DataContext
创建两个公共属性,然后从那里执行
绑定。不要忘记实现
INotifyPropertyChanged
,并在设置属性时调用实现。通过这种方式,其他开发人员可以更清楚地了解到,对于两个不同的控件,实际上有两个
datacontext

我的猜测是,您需要将datacontext专门设置为网格和列表框。大概是这样的:

WinUpdateDataGrid.DataContext = WindowsUpdate.updateClassList;


问题是,如果两个函数
CheckforWindowsUpdates\u Click
CheckforThirdPartyUpdatesButton\u Click
出现在同一个codebehind文件中,它们将设置相同的DataContext和绑定
ItemsSource=“{binding}”
在网格和列表框中将继承相同的DataContext。

不显示WinUpdateDataGrid或hirdPartyListBox的实际绑定。绑定显示在XAML片段中。ItemsSource=“{binding}”只表示ItemsSource已绑定-而不是绑定到的内容。显示第一个选项卡和第二个选项卡的整个XAML。他们的父母是什么?是否在一个大的XAML文件中定义了两个选项卡控件?我想看看it@Blam
{Binding}相当于{Binding Path=.},它绑定到当前源。
很有效,谢谢。我只是对我的datacontext分配不够明确。
WinUpdateDataGrid.DataContext = WindowsUpdate.updateClassList;
ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;