WPF根据数据项获取条目控件的方法-ItemContainerGenerator_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > WPF根据数据项获取条目控件的方法-ItemContainerGenerator

WPF根据数据项获取条目控件的方法-ItemContainerGenerator

 2017/1/9 5:37:53  TryBestToCode  程序员俱乐部  我要评论(0)
  • 摘要:一.方法:ContainerFromIndex:返回ItemCollection中指定索引处的项的容器。ContainerFromItem:返回与制定的项对应的容器(ComboxItem等条目控件)。Equals(Object):确定制定的Object是否等于当前的Object。Finalize:允许对象在垃圾回收对Object回收之前尝试释放资源并尝试其它清理操作。GeneratorPositionFromIndex:获取项在指定索引处的生成位置。GetHashCode
  • 标签:方法 item 数据 控件

一.方法:

ContainerFromIndex:返回 ItemCollection 中指定索引处的项的容器。

ContainerFromItem:返回与制定的项对应的容器(ComboxItem等条目控件)。

Equals(Object):确定制定的Object是否等于当前的Object。

Finalize:允许对象在垃圾回收对Object回收之前尝试释放资源并尝试其它清理操作。

GeneratorPositionFromIndex:获取项在指定索引处的生成位置。

GetHashCode:用作特定类型的哈希函数。

GetType:获取当前实例的Type。

IndexFromContainer:获取具有指定的生成的容器的项的索引。

IndexFromGeneratorPosition:返回映射到指定GereratorPosition的索引。

ItemFromContainer:返回与指定的容器生成的所对应的项。

 

二.事件

ItemsChanged : 当集合内的项发生变更时发生。

 

三.注意

 

class="LW_CollapsibleArea_Title">备注    ItemContainerGenerator 类维护项控件和其项容器 之间的关联。 has an associated <span class="selflink">ItemContainerGenerator, you will be able to retrieve it through a property on the control">如果某控件具有关联的ItemContainerGenerator,则能够通过该控件的属性检索它。

can use the <span class="selflink">ItemContainerGenerator to retrieve items based on their index or containers by specifying the data item.">您可以使用 ItemContainerGenerator 并基于项的索引来检索项或通过指定数据项来检索容器。 例如,如果您有一个绑定了数据的 Combox,并希望基于其索引获取一个 ComboxItem,则可以使用 ContainerFromIndex 方法。 如果想检索数据项,可使用 ItemFromContainer方法。

四.示例:

 

C#:

public MyTest()
{
    InitializeComponent();
    string[] myCollection= new string[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
   myCombox.DataContext = myCollection; } 
static int count = 1; 
private void Button_Click(object sender, RoutedEventArgs e)
{
ConmboxItem item = (ComboxItem)
  myCombox.ItemContainerGenerator.ContainerFromIndex(3); 
item.IsExpanded = true; if (count < 5) { item.Items.Add("Child " + count.ToString()); count++; }
}

 Xaml:

<StackPanel x:Name="LayoutRoot" Background="White">
    <Combox x:Name="myCombox" Width="200" ItemsSource="{Binding}" Margin="5"/>
    <Button Content="Add Child to Item 4" Width="150" Click="Button_Click"/>
</StackPanel>

 

 

发表评论
用户名: 匿名