1

Have a ListView with a template selector

What I need is for the TextBox named tbStringSVfieldValue to get focus when this ListBoxItem gets selected (clicked or arrow) ?

<DataTemplate x:Key="fieldTemplateStringSV">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Text="Enter key to save. Tab to get the first match from existing values.  Value is only committed on Enter key."  VerticalAlignment="Center" Style="{StaticResource Italic}"/>
        <ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" VerticalContentAlignment="Stretch" 
            HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
            <TextBox x:Name="tbStringSVfieldValue" BorderBrush="SteelBlue" BorderThickness="2" TextWrapping="Wrap" 
                Text="{Binding Path=FieldValue, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource stringTabConverter}}"
                Validation.Error="Validataion_Error" AcceptsReturn="True" AcceptsTab="True"
                PreviewKeyDown="tbStringSVfieldValue_PreviewKeyDown"/>
            <!--, Converter={StaticResource stringTabConverter}-->
        </ScrollViewer>
        <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" Text="Existing values. Click to populate the value above."  VerticalAlignment="Center" Style="{StaticResource Italic}"/>
        <ListBox Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" Height="150" Margin="5,2,0,0" x:Name="lbSVtextPast" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.IsDeferredScrollingEnabled="True"
                FontStyle="Normal"
                ItemsSource="{Binding Path=PastEntries}" SelectedItem="{Binding Path=SelectedPastEntry}"
                SelectionChanged="lbSVtextPastSelectionChanged"/>
    </Grid>
</DataTemplate>

above I tried

<Grid FocusManager.FocusedElement="{Binding ElementName=svTBsv}">
    <ScrollViewer x:Name="svTBsv" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" VerticalContentAlignment="Stretch" 
        HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"
        FocusManager.FocusedElement="{Binding ElementName=tbStringSVfieldValue}">
        <TextBox x:Name="tbStringSVfieldValue" BorderBrush="SteelBlue" 

note below does NOT work
this.tbStringSVfieldValueStringSV is null
it only exists in the template
need to somehow get access to the correct template
note there will be more than one DataTemplate x:Key="fieldTemplateStringSV" in the ListBox

private void lbCurDocFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{   
    MessageBox.Show("lbCurDocFields_SelectionChanged " + sender.GetType().ToString());
    if (sender is ListBox)
    {
        ListBox lb = (ListBox)sender;
        //lb.Template.VisualTree;
        MessageBox.Show(lb.Template.ToString());
        if (lb.SelectedItem != null)
        {
            MessageBox.Show(lb.SelectedItem.GetType().ToString());
            if (lb.SelectedItem is GabeLib.DocFieldStringSV)
            {
                if (this.tbStringSVfieldValueStringSV != null)
                    this.tbStringSVfieldValueStringSV.Focus();
            }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Could you add some more demo-code, so i can reproduce your issue? Its hard to guess whats going on without having a clue, what is bound – lokusking Jul 17 '16 at 15:58
  • @lokusking That will be a bit of code. Let me try and put something together. – paparazzo Jul 17 '16 at 16:25
  • Hold on. Try this one first: `FocusManager.SetFocusedElement(this.tbStringSVfieldValue, this.tbStringSVfieldValue);` – lokusking Jul 17 '16 at 17:02
  • @lokusking Problem is that this.tbStringSVfieldValueStringSV is null. The stuff in the template is not readily available. – paparazzo Jul 17 '16 at 17:11
  • I've tried this now since about 2 hours, because it made me curious. This seems really be impossible to handle with XAML only. The cleanest solution might be a behavior placed in the Textbox, listening to the ListBox's Changed event – lokusking Jul 17 '16 at 18:54
  • I am not looking for a XAML only solution. I am happy to handle it in the SelectionChanged event. – paparazzo Jul 17 '16 at 18:57
  • Something like [this](http://stackoverflow.com/a/31272370/4558029) came to my mind. Somewhere a bit down is a real behavior too – lokusking Jul 17 '16 at 19:14
  • @lokusking I have been hunting down that path too. If I can get to the ContentPresenter then can user can use FindResouce. – paparazzo Jul 17 '16 at 20:05

0 Answers0