したいのでアクセスのListViewItemsのコンポーネントのラインナップListView?

StackOverflow https://stackoverflow.com/questions/92328

  •  01-07-2019
  •  | 
  •  

質問

内のイベントで、そういった、特定のテキストボックス内のListViewItemのテンプレートを作成します。にーは以下のようなものです:

<ListView x:Name="myList" ItemsSource="{Binding SomeList}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <!-- Focus this! -->
                        <TextBox x:Name="myBox"/>

また、次のようにコード:

(myList.FindName("myBox") as TextBox).Focus();

私のように誤解され、 FindName() docsで返します null.

もの ListView.Items はならないので、このコースを含む私の行きビジネスオブジェクトとなListViewItems.

いずれも myList.ItemContainerGenerator.ContainerFromItem(item), またはnullを返します。

役に立ちましたか?

解決

その理由を理解するには ContainerFromItem 動作しなかった私にとって、ここにある。イベントハンドラが必要だこの機能は以下のようなものです:

var item = new SomeListItem();
SomeList.Add(item);
ListViewItem = SomeList.ItemContainerGenerator.ContainerFromItem(item); // returns null

後の Add()ItemContainerGenerator いすぐに容器の CollectionChanged イベントが対応させていただいており、非UI-ねじになります。ない非同期呼び出を待つUIのスレッドをコールバック関数を実行する実ListViewItem制御する。

通知するこ ItemContainerGenerator 攻撃にさらされる StatusChanged イベントが発火した後のすべてのコンテナを生成します。

しかし、今まで聞このイベントの判定を制御現したいの設定フォーカスまたはいません。

他のヒント

としての服は、myBoxテキストボックスが見つからなかったの呼び出FindName、ListView.しかし、を得ることができListViewItem在選択されたのVisualTreeHelperクラスのテキストボックスからListViewItem.うするとどうなるのかわかりません

private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (myList.SelectedItem != null)
    {
        object o = myList.SelectedItem;
        ListViewItem lvi = (ListViewItem)myList.ItemContainerGenerator.ContainerFromItem(o);
        TextBox tb = FindByName("myBox", lvi) as TextBox;

        if (tb != null)
            tb.Dispatcher.BeginInvoke(new Func<bool>(tb.Focus));
    }
}

private FrameworkElement FindByName(string name, FrameworkElement root)
{
    Stack<FrameworkElement> tree = new Stack<FrameworkElement>();
    tree.Push(root);

    while (tree.Count > 0)
    {
        FrameworkElement current = tree.Pop();
        if (current.Name == name)
            return current;

        int count = VisualTreeHelper.GetChildrenCount(current);
        for (int i = 0; i < count; ++i)
        {
            DependencyObject child = VisualTreeHelper.GetChild(current, i);
            if (child is FrameworkElement)
                tree.Push((FrameworkElement)child);
        }
    }

    return null;
}

だから、なくてはならない問題は直接関係のコンテンツの質問は、いずれも、回答回答いただいても結構です。ができている"アクセスのListViewItemsのコンポーネントのラインナップListView"より:

public static IEnumerable<ListViewItem> GetListViewItemsFromList(ListView lv)
{
    return FindChildrenOfType<ListViewItem>(lv);
}

public static IEnumerable<T> FindChildrenOfType<T>(this DependencyObject ob)
    where T : class
{
    foreach (var child in GetChildren(ob))
    {
        T castedChild = child as T;
        if (castedChild != null)
        {
            yield return castedChild;
        }
        else
        {
            foreach (var internalChild in FindChildrenOfType<T>(child))
            {
                yield return internalChild;
            }
        }
    }
}

public static IEnumerable<DependencyObject> GetChildren(this DependencyObject ob)
{
    int childCount = VisualTreeHelper.GetChildrenCount(ob);

    for (int i = 0; i < childCount; i++)
    {
        yield return VisualTreeHelper.GetChild(ob, i);
    }
}

なんなのかど慌しい、再帰を返し、そうに作った。となっていないのに使用 yield return を再帰的な文脈です。

行き来できますのViewTreeの目的ListViewItem 記録の設定に対応した細胞のリガからのヒットテストです。

同様にしてデータ化しますので、列ヘッダーの親ビューを比較し、一致は、細胞内のカラムです。ているbindの細胞の名前の列ヘッダ名として重要なおコンパレータを委譲/フィルター

例えば:HitResultはTextBlockました。ご希望の取扱いにListViewItem'.

enter image description here

/// <summary>
///   ListView1_MouseMove
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListView1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
  if (ListView1.Items.Count <= 0)
    return;

  // Retrieve the coordinate of the mouse position.
  var pt = e.GetPosition((UIElement) sender);

  // Callback to return the result of the hit test.
  HitTestResultCallback myHitTestResult = result => {
    var obj = result.VisualHit;

    // Add additional DependancyObject types to ignore triggered by the cell's parent object container contexts here.
    //-----------
    if (obj is Border)
      return HitTestResultBehavior.Stop;
    //-----------

    var parent = VisualTreeHelper.GetParent(obj) as GridViewRowPresenter;
    if (parent == null)
      return HitTestResultBehavior.Stop;

    var headers = parent.Columns.ToDictionary(column => column.Header.ToString());

    // Traverse up the VisualTree and find the record set.
    DependencyObject d = parent;
    do {
      d = VisualTreeHelper.GetParent(d);
    } while (d != null && !(d is ListViewItem));

    // Reached the end of element set as root's scope.
    if (d == null)
      return HitTestResultBehavior.Stop;

    var item = d as ListViewItem;
    var index = ListView1.ItemContainerGenerator.IndexFromContainer(item);
    Debug.WriteLine(index);

    lblCursorPosition.Text = $"Over {item.Name} at ({index})";

    // Set the behavior to return visuals at all z-order levels.
    return HitTestResultBehavior.Continue;
  };

  // Set up a callback to receive the hit test result enumeration.
  VisualTreeHelper.HitTest((Visual)sender, null, myHitTestResult, new PointHitTestParameters(pt));
}

を使用していま同様の手法とコンポーネントのラインナップの新規開発を維持-管理:

Private Sub SelectAllText(ByVal cell As DataGridCell)
    If cell IsNot Nothing Then
        Dim txtBox As TextBox= GetVisualChild(Of TextBox)(cell)
        If txtBox IsNot Nothing Then
            txtBox.Focus()
            txtBox.SelectAll()
        End If
    End If
End Sub

Public Shared Function GetVisualChild(Of T As {Visual, New})(ByVal parent As Visual) As T
    Dim child As T = Nothing
    Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)
    For i As Integer = 0 To numVisuals - 1
        Dim v As Visual = TryCast(VisualTreeHelper.GetChild(parent, i), Visual)
        If v IsNot Nothing Then
            child = TryCast(v, T)
            If child Is Nothing Then
                child = GetVisualChild(Of T)(v)
            Else
                Exit For
            End If
        End If
    Next
    Return child
End Function

その技術はかなり適用すぐの場合にはlistviewitem度で生成されます。

することができ易に行えるよ

private void yourtextboxinWPFGrid_LostFocus(object sender, RoutedEventArgs e)
    {
       //textbox can be catched like this. 
       var textBox = ((TextBox)sender);
       EmailValidation(textBox.Text);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top