Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在listbox中指定将源绑定到usercontrol_Wpf - Fatal编程技术网

Wpf 在listbox中指定将源绑定到usercontrol

Wpf 在listbox中指定将源绑定到usercontrol,wpf,Wpf,如何为用户控件指定数据源: <ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Stretch"> <ListBox ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate>

如何为用户控件指定数据源:

<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Stretch">
    <ListBox ItemsSource="{Binding}">                                
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ucMyControl />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</ScrollViewer>

ucMyControl如何知道要采用哪些值?我意识到lisbox将创建与bondong集合中的项目一样多的UCmyControl?
此外,ScrollViewer无法工作-我无法访问第三个控件(它们都是空的,但我希望您能告诉我如何绑定它)。

项控件中实例化的控件(例如
列表框
)将其属性自动设置为Items控件的
Items
ItemsSource
集合中的相应项

因此,例如,如果您有一组属性为
FirstName
LastName
Person
对象作为列表框项,您可以在UserControl中这样绑定:

<UserControl ...>
    <StackPanel>
        <TextBlock Text="{Binding Path=FirstName}"/>
        <TextBlock Text="{Binding Path=LastName}"/>
    </StackPanel>
</UserControl>


您不需要显式设置DataContext属性。

为什么我要指定TextBlocks?我已经有了用户控件以及我需要的控件和布局?是的,但我怎么知道呢。这只是一个如何绑定的示例。那么如何将数据绑定到usercontrol?我不知道你的意思。UserControl有一个DataContext属性,该属性是自动设置的,并且是控件内任何绑定路径的根