2017-11-15 16 views
0

ListBoxの最初の項目にフォーカスを設定できません。 ListBoxにはいくつかのコレクションバインドがあります。コードビハインドの最初のことは、コントロールの初期化があるので、私は常に-1SelectedIndexとしていたようです。 XAML私は何をしようとしているListBoxの最初の項目にフォーカスを設定できません

<Window x:Class="Labels.Szablony" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Labels" 
    mc:Ignorable="d" 
    Title="Labels" Height="350" Width="250" WindowStartupLocation="CenterScreen" 
    ResizeMode="CanMinimize" KeyDown="Window_KeyDown" Background="#FFF6A300" > 
<Window.DataContext> 
    <local:LabelGridViewModel/> 
</Window.DataContext> 
<ScrollViewer VerticalScrollBarVisibility="Hidden" VerticalContentAlignment="Center" 
       VerticalAlignment="Center" Background="#FFF6A300"> 
    <Grid Background="#FFF6A300" Name="labelGrid"> 
     <ListBox x:Name="SzablonyBox" ItemsSource="{Binding LabelsCollection, IsAsync=True}" 
       HorizontalAlignment="Center" VerticalAlignment="Top" 
      MinWidth="100" MinHeight="100" BorderBrush="{x:Null}" 
      Background="{x:Null}" TextSearch.Text="BLABLAB" 
      TextSearch.TextPath="Name" IsTextSearchEnabled="True" 
       VerticalContentAlignment="Center" Margin="0 25 0 0" 
       Width="250" HorizontalContentAlignment="Center"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="{x:Type ListBoxItem}"> 
        <EventSetter Event="PreviewKeyDown" 
           Handler="ListBoxItem_PreviewKeyDown"/> 
       </Style> 
      </ListBox.ItemContainerStyle> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <DockPanel Width="214"> 
         <TextBlock Text="{Binding Name}" 
           FontSize="12" Height="35" 
           VerticalAlignment="Center" 
           HorizontalAlignment="Center" 
           Width="250" 
           TextAlignment="Center"/> 
        </DockPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <TextBox x:Name="InputText" 
       Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" 
       HorizontalAlignment="Center" Height="22" TextWrapping="Wrap" 
       VerticalAlignment="Top" Width="250" Background="#FFF6A300" 
       IsEnabled="False" BorderBrush="#FFF6A300" 
       VerticalContentAlignment="Center" 
       HorizontalContentAlignment="Center" 
       Foreground="Black" SelectionBrush="{x:Null}"/> 
    </Grid> 
</ScrollViewer> 

がどのように見える

itemがヌルであるため、当然のことながら

public Labels() 
{ 
    InitializeComponent(); 

    LabelsBox.Focus(); 
    LabelsBox.SelectedIndex = 0; 

    var item = LabelsBox.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem; 

    item.Focus(); 
} 

例外が、そこにあります。私が言ったようにSelectedIndexは常に-1です。私は間違って何をしていますか?

+1

<ListBox ItemsSource="{Binding LabelsCollection}" ...> 

またLoadedイベントハンドラにそれを移動することで、できるだけ遅く、あなたの初期化コードを実行することができます。 LabelsBoxがSzablonyBoxであると仮定すると、ItemsSource Bindingは非同期であるため、コンストラクターの実行中はまだ使用可能なアイテムはありません。 – Clemens

+0

@Clemensそれは動作します。ありがとうございました。 – fanarek

答えて

1

ItemsSourceバインディングは非同期なので、コンストラクターの実行中にはまだ使用可能なアイテムはありません。デフォルトの動作すなわち、同期であることが

変更バインディング:

public Labels() 
{ 
    InitializeComponent(); 
    ... 

    Loaded += (s, e) => 
    { 
     var item = (ListBoxItem)LabelsBox.ItemContainerGenerator.ContainerFromIndex(0); 
     item?.Focus(); 
    }; 
} 
関連する問題