gpt4 book ai didi

wpf - 如何从 ItemsControl 的 Item 后面的代码访问 ItemsControl 的 ItemsSource 元素?

转载 作者:行者123 更新时间:2023-12-01 11:05:38 27 4
gpt4 key购买 nike

在我的主视图中,我有一个绑定(bind)到对象集合的 ItemsControl:

<ItemsControl ItemsSource="{Binding Path=Concepts}"
ItemTemplate="{StaticResource ActivationLevelTemplate}"
/>

ActivationLevelTemplate 只是另一个 View :

 <DataTemplate x:Key="ActivationLevelTemplate">
<view:ConceptActivationView Height="50"/>
</DataTemplate>

在这个 View 中有一个文本 block ,绑定(bind)到上述集合中的对象的属性。该属性显示正确,现在我需要从后面的 View 代码访问同一对象的其他属性。这看起来微不足道,但我无法让它发挥作用。

<TextBlock Text="{Binding Path=Name}"
HorizontalAlignment="Center"
/>
<d3:Plotter2D Name="Plotter"/>

我遇到的最好的事情是ItemContainerGenerator,但它似乎并不是我们所需要的。

最佳答案

重要的是您尝试访问该对象的上下文。例如,如果您处理 DataTemplate 中的事件,您可以轻松地从发送方的 DataContext 中获取对象(必须是 FrameworkElement),例如如果我要处理一个按钮点击:

private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (FrameworkElement)sender;
var employee = (Employee)button.DataContext;
//...
}

事实上,如果您的整个 View 都在 DataTemplate 中,您也可以直接从 View 的 DataContext 中获取对象。

关于wpf - 如何从 ItemsControl 的 Item 后面的代码访问 ItemsControl 的 ItemsSource 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6183987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com