简体   繁体   English

将UserControl绑定到ListBox ItemSource

[英]Binding a UserControl to a ListBox ItemSource

I have an ObservableCollection that has a list of objects. 我有一个ObservableCollection,其中包含对象列表。

I would like to bind this observable collection to the ListBox but not in a way that each object gets shown inside the ListView, but instead that Each object creates a UserControl that has the object itself as input parameter. 我想将此可观察的集合绑定到ListBox,但不希望每个对象都显示在ListView中,而是每个对象都创建一个UserControl,该对象本身具有输入参数。

To give a similar example; 举一个类似的例子; imagine a server browser where each line is a UserControl and data is stored in a List/ObservableCollection and then displayed inside a frontend ListBox. 想象一个服务器浏览器,其中每一行都是一个UserControl,数据存储在List / ObservableCollection中,然后显示在前端ListBox中。

I have this XAML code in my MainWindow.xaml 我的MainWindow.xaml中有此XAML代码

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding Bills}" ItemContainerStyle="{StaticResource BillStyle}">
</ListBox>

And this is the style that invokes the UserControl 这就是调用UserControl的样式

<Style TargetType="{x:Type ListBoxItem}" x:Key="BillStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ns:BillItem />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and in the MainWindow.xaml.cs I have: 在MainWindow.xaml.cs中,我具有:

ObservableCollection<Bill> Bills {get;set;}

And inside the constructor I just added few objects. 在构造函数内部,我仅添加了几个对象。

I would like that the Bill object gets pushed to the User Control in this manner: 我希望Bill对象以这种方式推送到用户控件:

UserControl BillItem (Bill BindedObject)
{ ... }

But I don't know how to do that. 但是我不知道该怎么做。

If I understood correctly, you need something like this: 如果我理解正确,则需要以下内容:

<ItemsControl ItemsSource="{Binding Path=ListOfObjects}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:MyUserControl MyProperty="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

MyUserControl needs to have dependency property, because you cant pass to constructor: MyUserControl需要具有依赖项属性,因为您不能传递给构造函数:

public object MyProperty
{
    get { return (object)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(object), typeof(MyUserControl), new PropertyMetadata(false));

on the view have an item that inherits from ItemsControl eg listbox, then place your objects in an ObservableCollection property in your VM. 在视图上,有一个从ItemsControl继承的项目,例如列表框,然后将对象放在VM的ObservableCollection属性中。 then simply bind to that property in the ItemsControl/listbox or whatevers ItemsSource property 然后只需绑定到ItemsControl / listbox或其他ItemsSource属性中的该属性

ItemSource="{Binding MyObjectCollection}" ItemSource =“ {Binding MyObjectCollection}”

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

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