在ListView控件在WPF访问控制 [英] Access control in ListView in WPF

查看:143
本文介绍了在ListView控件在WPF访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的列表视图,并想访问的选项的文本框在身边?

lsitview.blah.blah.findTheTextBox(嗒嗒)名称=好!。

我怎么能做到这一点?谢谢

 < ListView控件名称=列表视图>
  < ListView.View>
    < GridView控件>
      < GridViewColumn>
        < GridViewColumn.CellTemplate>
          <的DataTemplate>
             <文本框文本={结合}/>
          < / DataTemplate中>
        < /GridViewColumn.CellTemplate>
      < / GridViewColumn>
    < / GridView的>
  < /ListView.View>
< / ListView控件>
 

解决方案

没关系,我figoure出来:

  SequenceItem _item = dgSequence.SelectedItem为SequenceItem;
            如果(_item!= NULL)
                _item.IsReadOnly = FALSE;

            ListViewItem的_lstItem = dgSequence.GetCurrentListItem();
            如果(_lstItem!= NULL)
            {
                文本框_txt = _lstItem.GetDescendantByType<文本框>();
                如果(_txt!= NULL)
                {
                    _txt.GetBindingEx pression(TextBox.IsReadOnlyProperty).UpdateTarget();
                    _txt.Focus();
                    _txt.SelectAll();
                }
            }

       公共静态牛逼GetDescendantByType< T>(此可视元素),其中T:视觉
        {

            如果(元素== NULL)返回NULL;

            如果(元素T)返回(T)的元素;

            牛逼foundElement = NULL;

            如果(元素是FrameworkElement的)

                (元素作为FrameworkElement的).ApplyTemplate();

            的for(int i = 0;

                I< VisualTreeHelper.GetChildrenCount(元);我++)
            {

                视觉视觉= VisualTreeHelper.GetChild(单元,I)为可视。

                foundElement = GetDescendantByType< T>(视觉);

                如果(foundElement!= NULL)

                    打破;

            }

            返回foundElement;

        }

        公共静态的ListViewItem GetCurrentListItem(这ListView的lstvw)
        {
            如果(lstvw.SelectedIndex℃,)
                返回null;
            ListViewItem的_lstItem = lstvw.ItemContainerGenerator.ContainerFromIndex(lstvw.SelectedIndex)作为ListViewItem的;
            返回_lstItem;
        }
 

I have a listview like this and would like to access the textbox of SELECTED ITEM in side?

lsitview.blah.blah.findTheTextBox("blah").Name="good!";

How am I able to do that? Thanks

<ListView Name="listview">
  <ListView.View>
    <GridView>
      <GridViewColumn>
        <GridViewColumn.CellTemplate>
          <DataTemplate>
             <TextBox Text="{Binding}"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

解决方案

Never mind, I figoure it out:

 SequenceItem _item = dgSequence.SelectedItem as SequenceItem;
            if (_item != null)
                _item.IsReadOnly = false;

            ListViewItem _lstItem = dgSequence.GetCurrentListItem();
            if (_lstItem != null)
            {
                TextBox _txt = _lstItem.GetDescendantByType<TextBox>();
                if (_txt != null)
                {
                    _txt.GetBindingExpression(TextBox.IsReadOnlyProperty).UpdateTarget();
                    _txt.Focus();
                    _txt.SelectAll();
                }
            }

       public static T GetDescendantByType<T>(this Visual element) where T : Visual
        {

            if (element == null) return null;

            if (element is T) return (T)element;

            T foundElement = null;

            if (element is FrameworkElement)

                (element as FrameworkElement).ApplyTemplate();

            for (int i = 0;

                i < VisualTreeHelper.GetChildrenCount(element); i++)
            {

                Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;

                foundElement = GetDescendantByType<T>(visual);

                if (foundElement != null)

                    break;

            }

            return foundElement;

        }

        public static ListViewItem GetCurrentListItem(this ListView lstvw)
        {
            if (lstvw.SelectedIndex < 0)
                return null;
            ListViewItem _lstItem = lstvw.ItemContainerGenerator.ContainerFromIndex(lstvw.SelectedIndex) as ListViewItem;
            return _lstItem;
        }

这篇关于在ListView控件在WPF访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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