0

My XAML:

  <Grid Background="White">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="1*"/>
      <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <ScrollViewer Grid.Column="0">
      <StackPanel>
        <Grid Margin="5" Background="LightGray">
          <TextBlock>Grid1</TextBlock>
        </Grid>
        <TextBlock Margin="5">TextBlock1</TextBlock>
        <CheckBox Margin="5">CheckBox1</CheckBox>
        <Button Margin="5">Button1</Button>
      </StackPanel>
    </ScrollViewer>
    <StackPanel Grid.Column="1">
      <Grid Margin="5" Background="LightGray">
        <TextBlock>Grid2</TextBlock>
      </Grid>
      <TextBlock Margin="5">TextBlock2</TextBlock>
      <CheckBox Margin="5">CheckBox2</CheckBox>
      <Button Margin="5">Button2</Button>
    </StackPanel>
  </Grid>

That gives this output:

UI Output

Here's my problem. If Button2 has focus clicking on Grid2 or TextBlock2 will not change focus. However if Button1 has focus and clicking on Grid1 or TextBlock1, then focus will be removed from Button1. Why? For me it looks like when you surround a panel with a ScrollViewer the focus logic is broken.

Thanks in advance.

Morten Frederiksen
  • 5,114
  • 1
  • 40
  • 72

1 Answers1

2

I can answer you partially...

Is not that ScrollViewer breaks focus, when you use a ScrollViewer as a container and you click on some place that hasn't a control able to get focus, ScrollViewer itself get focus, so to avoid ScrollViewer to take focus you can disable the property IsTabStop of it.

I tried this, and ScrollViewer doesn't get focus when you click on a control without focus handling like a Grid or Textblock, but focusing is still behaving strange, that's why I said "partially" before.

Set IsTabStop to false on ScrollViewer and see it for yourself...

Also, you can take a look of this post, where a guy proposed a way to implement a "focus inspector", it's just a few lines of code.

Community
  • 1
  • 1
ClownCoder
  • 454
  • 6
  • 10