无法将注意力集中在ListBox的第一项上

时间:2017-11-15 06:43:12

标签: c# wpf listbox

我无法将注意力集中在我的ListBox的第一项上。 ListBox有一些集合绑定。看起来,首先在代码隐藏中执行其所有内容的所有东西都有控件初始化所以我总是fooB-1

XAML

SelectedIndex

我试图做的事情如下:

<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>

当然有一个例外,因为public Labels() { InitializeComponent(); LabelsBox.Focus(); LabelsBox.SelectedIndex = 0; var item = LabelsBox.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem; item.Focus(); } 为空。就像我说item总是SelectedIndex。我做错了什么?

1 个答案:

答案 0 :(得分:1)

ItemsSource Binding是异步的,因此在构造函数运行时肯定没有任何项可用。

将Binding更改为同步,即默认行为:

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

您也可以通过将初始化代码移动到Loaded事件处理程序来尽可能晚地执行初始化代码:

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

    Loaded += (s, e) =>
    {
        var item = (ListBoxItem)LabelsBox.ItemContainerGenerator.ContainerFromIndex(0);
        item?.Focus();
    };
}