将多个 ObjectDataProvider 与同一控件一起使用

发布于 2025-01-03 05:15:56 字数 1938 浏览 3 评论 0原文

我刚刚开始学习 WPF 中的绑定,并且在使用具有相同控件的多个 ObjectDataProvider 时遇到一些问题。

我有两个 ObjectDataProviders :

  1. 用于从数据库获取客户位置列表,用于填充 TreeView,
  2. 将位置作为参数并返回该位置的所有客户,填充 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 :

  1. Is used to get a list of customer locations from a database and is used to populate a TreeView and
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往昔成烟 2025-01-10 05:15:56

ObjectDataProviders 不太灵活,因为它们无法绑定。除此之外,您可以绑定到 TreeViewSelectedItem 并使用 Binding.Converter 根据该值为您提供正确的项目。

ObjectDataProviders are not very flexible as they cannot be bound. Among other things you could bind to the SelectedItem of the TreeView and employ a Binding.Converter to get you the items right items based on that value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文