将多个 ObjectDataProvider 与同一控件一起使用
我刚刚开始学习 WPF 中的绑定,并且在使用具有相同控件的多个 ObjectDataProvider 时遇到一些问题。
我有两个 ObjectDataProviders :
- 用于从数据库获取客户位置列表,用于填充 TreeView,
- 将位置作为参数并返回该位置的所有客户,填充 listView。
我想这样做,以便当我单击 TreeView 项目之一时,它将采用 SelectedItem 文本作为参数,使用它来填充列表视图。
<ObjectDataProvider
x:Key="getLocations"
ObjectType="{x:Type local:DataSetCreator}"
MethodName="getLocations"
/>
<ObjectDataProvider
x:Key="getCustomersFromLocation"
ObjectType="{x:Type local:DataSetCreator}"
MethodName="getCustomersFromLocation">
<ObjectDataProvider.MethodParameters>
<x:Static Member="System:String.Empty" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<TreeView HorizontalAlignment="Left"
Margin="12,12,0,12"
Name="treeView2" Width="186"
ItemsSource="{Binding Source={StaticResource getLocations}}" >
<TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Country}" />
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<ListView x:Name="lstCustomers"
ItemsSource="{Binding Source={StaticResource getCustomersFromLocation}}" Margin="204,41,12,12">
<ListView.View>
<GridView>
<GridViewColumn Header="CustomerID"
Width="200"
DisplayMemberBinding="{Binding Path=CustomerID}" />
<GridViewColumn Header="Company Name"
Width="370"
DisplayMemberBinding="{Binding Path=CompanyName}" />
</GridView>
</ListView.View>
</ListView>
是否可以在 XAML 中实现此目的,或者我是否需要使用隐藏代码?
I've just started to learn binding in WPF and am having some trouble with using multiple ObjectDataProviders with the same control.
I have two ObjectDataProviders :
- Is used to get a list of customer locations from a database and is used to populate a TreeView and
- Takes a location as a parameter and returns all the customers from that location, populating a listView.
I'd like to make it so that when I click on one of the TreeView items, that it would take the SelectedItem text as the parameter, use it to populate the listview.
<ObjectDataProvider
x:Key="getLocations"
ObjectType="{x:Type local:DataSetCreator}"
MethodName="getLocations"
/>
<ObjectDataProvider
x:Key="getCustomersFromLocation"
ObjectType="{x:Type local:DataSetCreator}"
MethodName="getCustomersFromLocation">
<ObjectDataProvider.MethodParameters>
<x:Static Member="System:String.Empty" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<TreeView HorizontalAlignment="Left"
Margin="12,12,0,12"
Name="treeView2" Width="186"
ItemsSource="{Binding Source={StaticResource getLocations}}" >
<TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Country}" />
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<ListView x:Name="lstCustomers"
ItemsSource="{Binding Source={StaticResource getCustomersFromLocation}}" Margin="204,41,12,12">
<ListView.View>
<GridView>
<GridViewColumn Header="CustomerID"
Width="200"
DisplayMemberBinding="{Binding Path=CustomerID}" />
<GridViewColumn Header="Company Name"
Width="370"
DisplayMemberBinding="{Binding Path=CompanyName}" />
</GridView>
</ListView.View>
</ListView>
Is it possible to achieve this within the XAML, or do I need to use the code-behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ObjectDataProviders
不太灵活,因为它们无法绑定。除此之外,您可以绑定到TreeView
的SelectedItem
并使用Binding.Converter
根据该值为您提供正确的项目。ObjectDataProviders
are not very flexible as they cannot be bound. Among other things you could bind to theSelectedItem
of theTreeView
and employ aBinding.Converter
to get you the items right items based on that value.