js css html

WPF 绑定到ObjectDataProvider 对象

2022-06-18  本文已影响0人  Ritchie_Li

命名空间添加对类的引用

xmlns:data="clr-namespace:StoreDatabase;assembly=StoreDatabase"

Window资源中 对象类型指定数据:类名。方法名称为类中的方法

<Window.Resources>

        <ObjectDataProvider ObjectType="{x:Type data:StoreDB}"                       

                      MethodName="GetProductsSlow" x:Key="products" IsAsynchronous="True"></ObjectDataProvider>

    </Window.Resources>

StoreDB 是一个数据库访问类:

public class StoreDB { ......}

直接引用该类的方法

public ICollection<Product> GetProductsSlow()

        {

            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));

            return GetProducts();

        }

UI设置列表绑定到 ObjectDataProvider对象:

<ListBox Grid.Row="1" x:Name="lstProducts" Margin="5" DisplayMemberPath="ModelName" ItemsSource="{Binding Source={StaticResource products}}"/>

Grid 绑定到列表指定对象:

<Grid DataContext="{Binding ElementName=lstProducts,Path=SelectedItem}">

其它控件可以绑定指定对象的属性字段

<TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox>

在绑定时候,使用类的方法或数据绑定到列表

上一篇 下一篇

猜你喜欢

热点阅读