如何将 itemtemplate 中的属性绑定到 itemsource 中的属性?

发布于 2024-10-07 10:41:05 字数 1285 浏览 0 评论 0原文

考虑这个容器:

public class ItemInfo : DependencyObject
{
    public string Name { get; set; }
    public ObservableCollection<SomeDataItem> DataValues { get; set; }
    ...
    Dependency object registration and event handling
    ...
}

public class MyItemSource : ObservableCollection<ItemInfo>
{
   ...
}

现在,我希望在列表视图中显示此数据,其中显示项目的控件是自定义的。为此,我将 MyItemSource 设置为 listview 的 ItemSource 并定义一个 ItemTemplate。但是,我似乎无法访问 ItemTemplate 中的 ItemInfo。这是我的 XAML:

    <Grid>
    <ListBox ItemsSource="{StaticResource MyStaticDataSource}" 
         Grid.IsSharedSizeScope="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ItemInfoUserControl x:Name="itemInfoUserControl"
                Name = "{Binding Name}"       <--- this doesn't work
                Data = "{Binding DataValues}" <--- this doesn't work
                Width="300" Height="200"  
                Grid.Row="1">
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    </Grid>

它不是绑定到 ItemSource 的 ItemInfo,而是绑定到 ItemInfoUserControl 属性,这不是我希望它执行的操作。有没有办法将 itemtemplate 中的属性绑定到 itemsource 中的属性?或者对于我最终想要实现的目标是否有其他方法?

谢谢!

Consider this container:

public class ItemInfo : DependencyObject
{
    public string Name { get; set; }
    public ObservableCollection<SomeDataItem> DataValues { get; set; }
    ...
    Dependency object registration and event handling
    ...
}

public class MyItemSource : ObservableCollection<ItemInfo>
{
   ...
}

Now, I wish to display this data in a listview where the control that displays the item is custom. For that, I'd set the MyItemSource to listview's ItemSource and define a ItemTemplate. However, it seems that I have no access to ItemInfo in the ItemTemplate. This is my XAML:

    <Grid>
    <ListBox ItemsSource="{StaticResource MyStaticDataSource}" 
         Grid.IsSharedSizeScope="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ItemInfoUserControl x:Name="itemInfoUserControl"
                Name = "{Binding Name}"       <--- this doesn't work
                Data = "{Binding DataValues}" <--- this doesn't work
                Width="300" Height="200"  
                Grid.Row="1">
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    </Grid>

Instead of binding to ItemSource's ItemInfo, it binds to the ItemInfoUserControl properties, which is not what I wish it to do. Is there a way to bind properties in itemtemplate to a property in itemsource ? Or is there an alternative approach to what i'm ultimately trying to accomplish ?

Thanks!

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

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

发布评论

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

评论(1

冰葑 2024-10-14 10:41:05

好吧,首先您需要使用正确的绑定语法

Name = "{Binding Name}"

Data = "{Binding DataValues}"

不仅仅是“Name =“Binding Name””和“Data =“Binding DataValues””。请注意在绑定表达式周围添加“{”和“}”。
只要 NameDataValuesItemInfoUserControl 中的 DependencyProperties,这可能足以解决您的问题。如果没有,您需要将它们实现为 DependencyProperties 以便能够在 XAML 中绑定到它们。请参阅此处,获取有关定义自定义依赖项属性的优秀 MSDN 文章。


编辑:另外,刚刚注意到 - 您同时设置了 x:NameName。来自 MSDN 上的这篇文章

如果 Name 可用作类的属性,则 Name 和 x:Name 可以作为属性互换使用,但如果在同一元素上指定两者,则会导致解析异常。如果XAML是标记编译的,则异常将发生在标记编译时,否则发生在加载时。

删除 x:Name="itemInfoUserControl" 并查看是否有帮助。

Well, first of all you need to use proper binding syntax:

Name = "{Binding Name}"

and

Data = "{Binding DataValues}"

Instead of just "Name = "Binding Name"" and "Data = "Binding DataValues"". Note the addition of "{" and "}" around your binding expression.
This might be enough to solve your problem, as long as Name and DataValues are DependencyProperties in ItemInfoUserControl. If not, you'll need to implement them as DependencyProperties in order to be able to bind to them in XAML. See here for a good MSDN article on defining custom dependency properties.


Edit: Also, just noticed -- you're setting both x:Name and Name. From this article on MSDN:

If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but a parse exception will result if both are specified on the same element. If the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.

Remove x:Name="itemInfoUserControl" and see if that helps.

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