zoukankan      html  css  js  c++  java
  • WPF怎么寻找在ItemTemplate内部的各种控件

    有如下控件

    <Grid>
            <ListBox x:Name="lb">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox x:Name="ck"></CheckBox>
                            <TextBlock x:Name="tb" Text="{Binding StuId}"></TextBlock>
                            <TextBlock Text="{Binding StuName}"></TextBlock>
                            <TextBlock Text="{Binding Sex}"></TextBlock>
                            <TextBlock Text="{Binding Pwd}"></TextBlock>
                            <Button Content="修改" Width="100" Height="30"></Button>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="Show" Width="100" Height="30" Click="Button_Click"></Button>
        </Grid>

     现要知道CheckBox是否选中,但是它是DataTemplate内部的控件,要如何访问到呢,MSDN上这样做的

    http://msdn.microsoft.com/zh-cn/library/bb613579(v=vs.100).aspx

    所以方法如下

    private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is childItem)
                        return (childItem)child;
                    else
                    {
                        childItem childOfChild = FindVisualChild<childItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
                return null;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ListBoxItem lbitem = null;
                ContentPresenter myContentPresenter = null;
                DataTemplate myDataTemplate = null;
                CheckBox ck = null;
                TextBlock tb = null;
                for (int i = 0; i < lb.Items.Count; i++)
                {
                    lbitem = (ListBoxItem)(lb.ItemContainerGenerator.ContainerFromItem(lb.Items[i]));
                    myContentPresenter = FindVisualChild<ContentPresenter>(lbitem);
                    myDataTemplate = myContentPresenter.ContentTemplate;
                    ck = (CheckBox)myDataTemplate.FindName("ck", myContentPresenter) as CheckBox;
                    if (ck.IsChecked == true)
                    {
                        tb = (TextBlock)myDataTemplate.FindName("tb", myContentPresenter) as TextBlock;
                        MessageBox.Show(tb.Text);
                    }
                }
            }

     功能实现...

    今天又遇到一个不一样的,DataGrid

    好吧,上源代码

    <DataGrid x:Name="dataGrid5"
                          AutoGenerateColumns="False"
                          AlternatingRowBackground="LightSteelBlue"
                          RowBackground="Azure"
                          IsReadOnly="True">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox x:Name="ck"></CheckBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding StuName}"></TextBlock>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="StuName">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" 
                                                VerticalAlignment="Center">
                                        <TextBlock Padding="5,0,5,0"
                                                   Text="{Binding Resume}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" 
                                                VerticalAlignment="Center">
                                        <TextBlock Padding="5,0,5,0"
                                                   Text="{Binding Resume}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

     不错,我们要找的就是ck这个控件,并根据他是否选中来做一些业务处理,我主要是遍历,要获取某一行的checkbox是否被选中,请看这里http://blog.sina.com.cn/s/blog_541e699d0100wjs9.html

    DataGridTemplateColumn _dataTemplate = dataGrid5.Columns[0] as DataGridTemplateColumn;
                FrameworkElement element = null;
                CheckBox ck=null;
                for (int i = 0; i < dataGrid5.Items.Count-1; i++)
                {
                    element = _dataTemplate.GetCellContent(dataGrid5.Items[i]);
                    ck=_dataTemplate.CellTemplate.FindName("ck",element) as CheckBox;
                    if (ck.IsChecked == true)
                    {
                        MessageBox.Show(string.Format("StuName:{0}",(dataGrid5.Items[i] as Stu).StuName));
                    }
                }

    ok,又搞定...

  • 相关阅读:
    CSS实现返回网页顶部
    jQuery实现小火箭动态返回顶部代码
    Linux目录结构介绍
    Linux常用命令及技巧
    Linux文件系统
    Linux特性
    numpy中基础函数
    restful规范
    堆栈
    三次握手与四次挥手
  • 原文地址:https://www.cnblogs.com/HelloMyWorld/p/2981706.html
Copyright © 2011-2022 走看看