WrapPanel从孩子身上抓住焦点

时间:2015-06-19 06:47:42

标签: c# xaml silverlight

我在scrollviewer的一个wrappanel中有一些(自制)控件,我想让我的控件在我点击它们或Tab键时获得焦点。但是包装纸抓住焦点并交给儿童[0]。

<ScrollViewer IsTabStop="False" >
   <toolkit:WrapPanel>
        <!-- children, filled in from code. IsTabStop="True" -->       
   </toolkit:WrapPanel>
</ScrollViewer>

scrollviewer做了同样的事情,但它的IsTabStop="False"使它表现得很好。但是,WrapPanel没有IsTabStop属性。那么我怎么能让它停止从孩子那里获得焦点呢?

我尝试使用Focus()从我的控件中的鼠标单击事件处理程序手动设置焦点#39;该控件获得焦点,但是wrappanel立即抓住它,它最终在Children [0],即使我试图阻止鼠标点击事件冒泡(e.Handled = true)。

2 个答案:

答案 0 :(得分:1)

不知怎的,它对我来说很好。分享我尝试过的代码片段。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image Name="img" Height="100" Width="100"/>
        <TextBox Name="tb3" Text="text2" KeyDown="tb3_KeyDown"  Width="200"/>
    </Grid>
    <ScrollViewer Grid.Row="2">
    <toolkit:WrapPanel Orientation="Vertical" Name="wp">

    </toolkit:WrapPanel>
    </ScrollViewer>

从代码中添加文本框

TextBox tb2 = new TextBox();
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        TextBox tb = new TextBox();
        tb.Text = "some text";
        tb.Width = 200;


        tb2.Text = "some text";
        tb2.Width = 200;

        wp.Children.Add(tb); 
        wp.Children.Add(tb2);

    }

将焦点从网格内的一个文本框更改为WrapPanel内的文本框

  private void tb3_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
if(e.Key == System.Windows.Input.Key.Enter)
{
tb2.Focus();
}
}

答案 1 :(得分:0)

我找到了解决方法。在清理了定制的用户控件并获得对所有焦点要求(IsTabStop,``等)的控制之后,WrapPanel仍然抓住了焦点,并在我明确将焦点设置为其中一个孩子。停止鼠标点击事件修复它:

void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs a)
{
   try
   {
       this.Focus();
   }
   finally
   {
       a.Handled = true;
   }
}
相关问题