silverlight - Silverlight如何在数据模板中使用按钮时提升列表框的选定项目

标签 silverlight mvvm listbox datatemplate

在列表框控件中,我有一个包含文本和按钮的数据模板。鉴于Silverlight/WPF的性质,当我单击列表框项目中的按钮时,在选择列表框项目之前会捕获按钮事件。因此,如果我尝试传递所选列表框项目的记录ID,则当前只能通过首先单击并选择列表框项目,然后单击按钮来进行。

有没有一种方法可以促进对列表框项目的选择,因此在创建列表框项目时,我可以单击列表框项目中的按钮,并可以调用某些事件(selectionChanged?),这将允许我捕获所选内容记录id并将其用于其他操作(作为方法中的参数传递等等)。我为此实现使用了简单MVVM工具包,因此我想知道是否可以在viewModel中进行处理,或者是否需要在后面的控件代码中进行处理,然后将选择插入viewModel。

列表框控件显示为:

<ListBox x:Name="ResultListBox"
             HorizontalAlignment="Stretch"
             Background="{x:Null}"
             Grid.Row="1"
             BorderThickness="0" HorizontalContentAlignment="Stretch"
             ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
             ItemsSource="{Binding SearchResults[0].Results}"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             Style="{StaticResource ListBoxStyle1}">

        <ListBox.ItemTemplate>

            <DataTemplate>
                <dts:TypeTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch">
                    <!--  Template 1  -->
                    <formatter:TypeTemplateSelector.CFSTemplate>
                        <DataTemplate>
                            <qr:ucIndex_Product />
                        </DataTemplate>
                    </formatter:TypeTemplateSelector.CFSTemplate>

                    <!--  Template 2  -->
                    <formatter:TypeTemplateSelector.PersonTemplate>
                        <DataTemplate>
                            <qr:ucIndex_Person  />
                        </DataTemplate>
                    </formatter:TypeTemplateSelector.PersonTemplate>

            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在数据模板(用户控件)中,包含按钮以及许多其他字段。除非有要求,否则我暂时将省略该代码。

提前致谢!

最佳答案

把它放在你的ListBox.Resources

<Style TargetType="{x:Type ListBoxItem}">
    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>

而这在后面的代码中
protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
    ListBoxItem item = (ListBoxItem)sender;
    item.IsSelected = true;
}

您也可以使用以下代码,而不使用后台代码,但是只要它具有KeyBoard焦点,它只会保持ListBoxItem处于选中状态。焦点移开后,该项目变为未选中状态
<Style TargetType="ListBoxItem">
  <Style.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
      <Setter Property="IsSelected" Value="True" />
    </Trigger>
  </Style.Triggers>
</Style>

编辑

由于Silverlight没有EventSetter,因此您可以使用ListBox的Loaded事件,并将以下内容添加到代码中:
private void ResultListBox_Loaded(object sender, RoutedEventArgs e)
{
    ListBox list = (ListBox)sender;
    list.GotFocus += ResultListBox_GotFocus;
}

void ResultListBox_GotFocus(object sender, RoutedEventArgs e)
{
    var item = FindAncester<ListBoxItem>((DependencyObject)e.OriginalSource);
    if (item != null) item.IsSelected = true;
}

T FindAncester<T>(DependencyObject current) 
    where T : DependencyObject
{
    current = VisualTreeHelper.GetParent(current);

    while (current != null)
    {
        if (current is T)
        {
            return (T)current;
        }
        current = VisualTreeHelper.GetParent(current);
    };
    return null;
}

这将捕获ListBox的Focus事件,获取触发焦点事件的控件,并遍历可视树以查找ListBoxItem对象,并将其Selected值设置为true。

关于silverlight - Silverlight如何在数据模板中使用按钮时提升列表框的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7013538/

相关文章:

c# - 视觉效果应该在 View 还是 ViewModel 中实现?

c# - 模型在 "traditional"C#/SQLite MVVM 应用程序中的作用

c# - 如何将组合框选择更改值与列表框项绑定(bind)?

wpf - 任何人都知道如何从使用 DataTemplate 作为其列表框项的 WPF 列表框中删除默认突出显示颜色?

C#:如何获取列表框中所选项目(和文本)的索引

silverlight - 如何跨域访问 Silverlight XAP 文件?

silverlight - 输入一天中的时间或持续时间的最直观、最有用的方法是什么?

c# - 来自数据绑定(bind)的 WPF MVVM 表布局

Silverlight:不能使用反射来跨 CAP 获取字段的值?

wpf - 为什么依赖属性?