简体   繁体   English

如何在 Visual Studio 2010 中使用对象数据源进行 WPF 数据绑定?

[英]How is WPF Data Binding using Object Data Source in Visual Studio 2010 done?

This is probably mostly a question about how to use the VS 2010 IDE tools in a way the Microsofties didn't specifically intend.这可能主要是关于如何以 Microsofties 没有特别打算的方式使用 VS 2010 IDE 工具的问题。 But since this is something I immediately tried without success.但由于这是我立即尝试但没有成功的事情。

I have defined a .NET 4.0 WPF Application project with a simple class that looks like this:我已经定义了一个 .NET 4.0 WPF 应用程序项目,其中包含一个如下所示的简单类:

Public Class Class1
    Public Property One As String = "OneString"
    Public Property Two As String = "TwoString"
End Class

I then defined it as an "Object Data Source" in VS2010, using the IDE's "Add New Data Source..." feature.然后我在 VS2010 中将其定义为“对象数据源”,使用 IDE 的“添加新数据源...”功能。 This exposes the class members in a GUI element in the IDE as given in this image:这将在 IDE 中的 GUI 元素中公开类成员,如下图所示: 数据源工具图片
(source: finitesolutions.com ) (来源: finitesolutions.com

Dragging "Class1" from that tool to the surface of "Window1.xaml" in a default "WPF Application" results in the design view looking like this:将“Class1”从该工具拖到默认“WPF 应用程序”中的“Window1.xaml”表面会导致设计视图如下所示:

从数据源工具中拖出
(source: finitesolutions.com ) (来源: finitesolutions.com

And generated XAML like this:并生成这样的 XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="133" Width="170" xmlns:my="clr-namespace:WpfApplication1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
>
<Window.Resources>
    <CollectionViewSource x:Key="Class1ViewSource" d:DesignSource="{d:DesignInstance my:Class1, CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource Class1ViewSource}" HorizontalAlignment="Left" Name="Grid1" VerticalAlignment="Top">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Label Content="One:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
    <TextBlock Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="OneTextBlock" Text="{Binding Path=One}" VerticalAlignment="Center" />
    <Label Content="Two:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
    <TextBlock Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="TwoTextBlock" Text="{Binding Path=Two}" VerticalAlignment="Center" />
</Grid>

Note the data bindings Text="{Binding Path=One}" and Text="{Binding Path=Two}" in the TextBlock elements.请注意TextBlock元素中的数据绑定Text="{Binding Path=One}"Text="{Binding Path=Two}"

Code-behind for Window1.xaml has this in Window_Loaded : Window_Loaded代码隐藏在Window_Loaded有这个:

   Class Window1
        Private m_c1 As New Class1
        Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            Dim Class1ViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("Class1ViewSource"), System.Windows.Data.CollectionViewSource)
            'Load data by setting the CollectionViewSource.Source property:
            'Class1ViewSource.Source = [generic data source]
            Me.DataContext = m_c1
        End Sub
    End Class

Running the application produces this output:运行应用程序会产生以下输出:

运行应用程序
(source: finitesolutions.com ) (来源: finitesolutions.com

The expected result was that "OneString" would appear next to "One" and "TwoString" next to "Two" in the running window.预期的结果是“OneString”将出现在运行窗口中的“One”旁边和“TwoString”旁边。

The question is: Why didn't this work?问题是:为什么这不起作用? What will work instead?什么会起作用? If I put bindings in a DataTemplate, it works.如果我将绑定放在 DataTemplate 中,它就可以工作。 Blend, with its sample data stuff, implied that this should work, but it doesn't. Blend 及其示例数据内容暗示这应该有效,但事实并非如此。 I know I'm missing something pretty fundamental here;我知道我在这里遗漏了一些非常基本的东西; what is it?它是什么?

I found this answer relatively quickly, so I'll answer my own question.我相对较快地找到了这个答案,所以我将回答我自己的问题。

The generated XAML code contains this:生成的 XAML 代码包含以下内容:

<Grid DataContext="{StaticResource Class1ViewSource}"

Removing the DataContext="{StaticResource Class1ViewSource}" removed something that had masked the Window.DataContext, and the binding worked.删除DataContext="{StaticResource Class1ViewSource}"删除了掩盖 Window.DataContext 的内容,并且绑定起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM