【问题标题】:How to programatically select an item in a data bound ListBox control如何以编程方式选择数据绑定 ListBox 控件中的项目
【发布时间】:2012-01-02 00:02:46
【问题描述】:

我有一个自定义样式的列表框:

<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 更改?

【问题讨论】:

    标签: c# silverlight windows-phone-7 listbox


    【解决方案1】:

    我已经通过连接到 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);
    
        }
    }
    

    对我来说似乎有点像蛮力,但它可以工作,并且会一直循环,直到找到所需的元素。

    希望对某人有所帮助

    【讨论】:

      【解决方案2】:

      在 WPF 中,您永远不应手动获取或设置 SelectedIndex。相反,设置绑定到控件的ListCollectionView 的当前项。选定的索引绑定到集合视图的当前项。

      【讨论】:

      • 我该怎么做呢?可以发个例子吗?
      • 没关系,SelectedIndex 确实有效,它只是不会改变列表框项的视觉状态。我编辑了我原来的问题。
      猜你喜欢
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多