列表框项数据模板绑定

发布于 2024-12-11 17:09:31 字数 838 浏览 0 评论 0原文

我有以下列表框及其项目。

<ListBox Name="listBox" Width="Auto" Height="Auto" Margin="10" ItemsSource="{Binding MyListItems}">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:Name="dt">
                    <controls:MyTreeControl x:Name="myTree"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
 </ListBox>
public class MyListItem
    {
        public string Name { get; set; }
        public string ID { get; set; }
    }

MyListItems 是 MyListItem 类型的集合,MyTreeControl 是一个以 ID 作为依赖属性的用户控件。

现在,我想在加载时将每个 MyListItem 的 ID 属性分配给 MyTreeControl 的 ID 属性。

<controls:MyTreeControl x:Name="myTree" ID={<I want to bind to MyListItem.ID>}/>

如何做到这一点?

I have following list box and its item.

<ListBox Name="listBox" Width="Auto" Height="Auto" Margin="10" ItemsSource="{Binding MyListItems}">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:Name="dt">
                    <controls:MyTreeControl x:Name="myTree"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
 </ListBox>
public class MyListItem
    {
        public string Name { get; set; }
        public string ID { get; set; }
    }

The MyListItems is collection of type MyListItem and MyTreeControl is a user control which has ID as a dependency property.

Now, I want to assign each MyListItem's ID property to MyTreeControl's ID property while it is loaded.

<controls:MyTreeControl x:Name="myTree" ID={<I want to bind to MyListItem.ID>}/>

How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

徒留西风 2024-12-18 17:09:31

由于绑定到 listBox 的每个项目都是 MyListItem 类型,因此应该很简单

<controls:MyTreeControl 
                     x:Name="myTree" 
                     ID="{Binding ID}" />

Since each item bound to the listBox is of type MyListItem It should be simply

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