簡體   English   中英

如何以編程方式在數據綁定的ListBox控件中選擇一個項目

[英]How to programatically select an item in a data bound ListBox control

我有一個自定義樣式的ListBox:

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="LayoutsListItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Height="170" Width="170" Margin="0,0,20,20">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="LayoutStates">
                                <VisualState x:Name="BeforeUnloaded"/>
                                <VisualState x:Name="BeforeLoaded"/>
                                <VisualState x:Name="AfterLoaded"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="border">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Thickness>4</Thickness>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="border" Width="170" Height="170" BorderBrush="{StaticResource PhoneAccentBrush}">
                            <Grid>
                                <Image Source="{Binding ThumbnailPath}" Width="170" Height="170" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

<ListBox x:Name="LayoutsList" ItemContainerStyle="{StaticResource LayoutsListItemStyle}" ItemsSource="{Binding}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit:WrapPanel Orientation="Horizontal" MaxWidth="410" />
         </ItemsPanelTemplate>
     </ListBox.ItemsPanel>
 </ListBox>

它會在所選列表框項目上方顯示邊框(手動選擇時)。 我希望列表框中的項目像單選按鈕一樣工作,並且列表框中的第一個項目默認為選中狀態。

我試圖像這樣設置列表框的SelectedIndex:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    // Loads a list of available Layouts to a ListBox

    XDocument layoutSummary = XDocument.Load("Content/LayoutSummary.xml");

    var layouts =
        from elem in layoutSummary.Descendants("ComicLayout")
        select new ComicLayout
        {
            Name = (string)elem.Attribute("Name").Value,
            FriendlyName = (string)elem.Attribute("FriendlyName").Value,
            ThumbnailPath = "Content/LayoutIcons/" + (string)elem.Attribute("Name").Value + ".png"
        };

    LayoutsList.DataContext = layouts;

    LayoutsList.SelectedIndex = 1;
}

但它似乎沒有任何作用。

如何以編程方式選擇數據綁定ListBox中的第一個(或任何其他)項目?

編輯

事實證明,SelectedIndex實際上有效,我可以對其進行控制,並根據需要將數據拉出ListBox。

所以我想問題是:

如何以編程方式觸發列表框項上的VisualState更改?

我設法通過掛接到ListBox控件上的LayoutUpdated事件來實現此目的

<ListBox x:Name="LayoutsList" ItemContainerStyle="{StaticResource LayoutsListItemStyle}" ItemsSource="{Binding}" LayoutUpdated="LayoutsList_LayoutUpdated">

LayoutsList_LayoutUpdated()

private void LayoutsList_LayoutUpdated(object sender, EventArgs e)
{
    if ((ListBoxItem)LayoutsList.ItemContainerGenerator.ContainerFromIndex(LayoutsList.SelectedIndex) != null)
    {
        ListBoxItem selectedItem = (ListBoxItem)LayoutsList.ItemContainerGenerator.ContainerFromIndex(LayoutsList.SelectedIndex);

        VisualStateManager.GoToState(selectedItem, "Selected", true);

    }
}

在我看來,這有點像蠻力,但它仍然有效,並且會一直循環播放,直到找到所需的元素為止。

希望可以幫助某人

在WPF中,永遠不要手動獲取或設置SelectedIndex 而是,設置綁定到控件的ListCollectionView的當前項。 所選索引綁定到集合視圖的當前項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM