<Window.Resources>
        <DataTemplate x:Key="PathTemplate">
            <TextBox Width="220"></TextBox>
        </DataTemplate>
 </Window.Resources>
这里面的TextBox没有名称,因为我要使用该模板在ListView中创建很多TextBox,如何获得当光标放入某个创建的TextBox内后,能够获得该TextBox的右键点击事件?谢谢!

解决方案 »

  1.   

    <TextBox Height="23" Margin="173,0,266,115" Name="textBox1" VerticalAlignment="Bottom" TextChanged="xxx" />把xxx换成你的事件
      

  2.   


    因为我的ListView中有一列都是TextBox,这一列TextBox如上面的定义,是没有名字的,如何对某一个TextBox传值?
      

  3.   

    通过查找VisualTree,可以得到所有的TextBox        private void GetTextBox(DependencyObject dpObj, List<TextBox> tbList)
            {
                for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dpObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(dpObj, i);
                    TextBox tb = child as TextBox;
                    if (tb != null)
                    {
                        if(!tbList.Contains(tb))
                        {
                            tbList.Add(tb);
                        }
                    }
                    else 
                    {
                        GetTextBox(child,tbList);
                    }
                }
            } private void OnCollectAllTextBox(object sender, RoutedEventArgs e)
            {
                List<TextBox> tbList = new List<TextBox>();
               GetTextBox(listView1, tbList);            tbList.ForEach(tb =>
                    {
                        //Register mouse right button down event
                        tb.PreviewMouseRightButtonDown += (s,arg)=>
                        {
                            tb.Text = "Mouse right button down";
                            //Cancel the event routing
                            arg.Handled = true;
                        };
                    });
            }
      

  4.   

    下个blend学学触发器 leftmousebuttondown事件 都可以