从usercontrol访问项源对象 [英] Accessing items source object from usercontrol

查看:47
本文介绍了从usercontrol访问项源对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Xaml中有这个代码



 <   ItemsControl    保证金  =  10    DockPanel.Dock   =   
< span class =code-attribute> ItemsSource = {Binding SnippetList} >
< ItemsControl.ItemTemplate >
< DataTemplate >
< v:MyUserControl 保证金 = 2 / >
< / DataTemplate >
< / ItemsControl.ItemTemplate >
< / ItemsControl >





如何访问绑定的SnippetList对象从MyUserControl的代码后面的MyUserControl。我知道这是一个简单的问题,但请帮助。



这样的东西就是我需要的东西

SnippetList是一个SnippetObject的ObservableCollection



 SnippetObject temp =  this  .getSnippetObject();  //  这是指MyUserControl  





我需要SnippetObject中的对象从一些计算中使用它而不是用于绑定它。

解决方案

如果你想访问你的ItemsControl UserControl代码隐藏文件,您需要为ItemsControl指定一个名称:

 <   itemscontrol     x:name   =  MyItemsControl Items =   路径  = 项目}      xmlns:x   = #unknown    /  >  





然后使用此名称访问ItemsControl:



  foreach  var  item  in  MyItemsControl.Items)
{

}





如果要从UserControl外部访问ItemsControl,首先需要在UserControl上定义属性,然后使用该属性访问ItemsControl。



UROS


So I have this Code in Xaml

<ItemsControl Margin="10" DockPanel.Dock="Left"
                 ItemsSource="{Binding SnippetList}">
                       <ItemsControl.ItemTemplate>
                           <DataTemplate>
                               <v:MyUserControl Margin="2"/>
                           </DataTemplate>
                       </ItemsControl.ItemTemplate>
                   </ItemsControl>



How can I access the object of SnippetList which is bound to a MyUserControl from MyUserControl's code behind. I know it is a simple question but please help.

Something like this is what I require
SnippetList is an ObservableCollection of SnippetObject

SnippetObject temp = this.getSnippetObject(); //this refers to MyUserControl



I need objects in SnippetObject to use it from some calculations and not for binding it.

解决方案

If you want to access your ItemsControl in the UserControl code-behind file, you need to give the ItemsControl a name like:

<itemscontrol x:name="MyItemsControl Items=" path="Items}"" xmlns:x="#unknown" />



Then use this name to access the ItemsControl:

foreach(var item in MyItemsControl.Items)
{

}



If you want to access the ItemsControl from outside of the UserControl you first need to define a property on your UserControl and than use that property to access the ItemsControl.

Uroš


这篇关于从usercontrol访问项源对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆