开发者

WPF custom control objects databinding to List(T) based on query

In a window of my WPF application I have several hundreds of objects, they based on a custom control. They differ from each other only by name:

...
<MyNamespace:MyCustControl x:Name="x4y3" />
<MyNamespace:MyCustControl x:Name="x4y4" />
<MyNamespace:MyCustControl x:Name="x4y5" />
<MyNamespace:MyCustControl x:Name="x4y6" />
<MyNamespace:MyCustControl x:Name="x4y7" />
...

The custom control and, consequently, these objects have some number of properties. The values of these properties should be updated from a List(T) (or another type of generic collection) which is populated from a SQL Server database using LINQ to SQL query.

In order to distinguish which record corresponds to each object, we have a field in a collection with names of these objects: "x4y3", "x4y4", "x4y5", ... and so on.

My question is:

Is it possible to establish databinding conn开发者_StackOverflowection of the total number of these objects to this generic collection, based on a condition of equality of the name of each object and the value of the corresponding field in generic collection?


This is entirely possible using an ItemControl in conjunction with a DataTemplate. Instead of creating an explicit list of your custom control, create an ItemsControl and bind its ItemsSource property to your collection. Set the ItemTemplate to a DataTemplate containing your custom control.

Example:

<Window.Resources>
    <DataTemplate x:Key="MyTemplate">
        <MyNamespace:MyCustControl/>
    </DataTemplate>
</Window.Resources>
...
    <ItemsControl ItemsSource="{Binding MyCollection}" 
                  ItemTemplate="{StaticResource MyTemplate}"/>

To display values from your data object simply bind the elements in your custom control as they will each have a different data object as their DataContext.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜