Silverlight,以编程方式从数据模板获取 ItemSource 数据
我有一个 ItemsControl,其 ItemsSource 为 Hours。 我用边框表示每个项目(在数据模板中)。
现在,每个边界都有一个小时的数据,我想在后面的代码中检索该数据。 有可能吗?
我的代码示例:
<ItemsControl x:Name="dayHours">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Name="dayHourBorder" Height="30" BorderBrush="#B0B6BE" Width="193" BorderThickness="1,0,1,1" Background="AliceBlue" Tag="{Binding Index}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
并且以天真的方式,我期望这样的代码:
(sender as Border).hourTime;
I've got an ItemsControl with an ItemsSource of Hours.
I represent each item by a border (in the data template).
Now, each of those border has an hour data, and i want to retrieve that in code behind.
is it even possible?
my code example:
<ItemsControl x:Name="dayHours">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Name="dayHourBorder" Height="30" BorderBrush="#B0B6BE" Width="193" BorderThickness="1,0,1,1" Background="AliceBlue" Tag="{Binding Index}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And in the naive way, I would expect a code like:
(sender as Border).hourTime;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中,我猜测您有兴趣在事件处理程序中查找“hourTime”?当 ItemsControl 为每个项目创建 DataTemplate 的“实例”时,它将模板的 DataContext 设置为项目本身。因此,以下内容应该有效:
From your code, I am guessing that you are interested in finding the 'hourTime' in an event handler? When an ItemsControl creates an 'instance' of your DataTemplate for each item, it sets the DataContext of the template to the item itself. Therefore the following should work: