【问题标题】:MVVM - Select item in ListBox with Double click and make boldMVVM - 通过双击选择列表框中的项目并加粗
【发布时间】:2012-08-20 05:26:42
【问题描述】:

我想这样做,所以需要双击才能选择 ListBox 中的项目。此选定项目应始终为粗体。我知道 SelectedItem 属性将不再反映我将其视为所选项目的项目,因此我之前用于使所选项目加粗的 XAML 将不再起作用。

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

我研究了如何使用 MVVM 处理双击,并得出结论认为可以使用后面的代码和 MouseDoubleClick 事件。

private void lbProfiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    _viewModel.SelectedProfile = ((ListBox)sender.)SelectedItem as MyProfile;
    //What should go here?
}

我的视图模型将有一个 SelectedProfile 属性,我认为该属性将在上述方法中设置。无论如何要在 XAML 中绑定 SelectedProfile 还是必须在后面的代码中进行管理?另外,让这个项目加粗的最佳方法是什么?


编辑 1:

我最终稍微调整了 Rachel 的答案,以便单击该项目会突出显示但未选中。这样视图模型就可以有一个 SelectedItem 属性和一个 HighlightedItem 属性。

private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount < 2)
        e.Handled = true;

    var clickedItem = ((ContentPresenter)e.Source).Content as MyProfile;

    if (clickedItem != null)
    {
        //Let view model know a new item was clicked but not selected.
        _modelView.HighlightedProfile = clickedItem;

        foreach (var item in lbProfiles.Items)
        {
            ListBoxItem lbi = 
                lbProfiles.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;

            //If item is not displayed on screen it may not have been created yet.
            if (lbi != null)
            {
                if (item == clickedItem)
                {
                    lbi.Background = SystemColors.ControlLightBrush;
                }
                else
                {

                    lbi.Background = lbProfiles.Background;
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    仅在DoubleClick 上选择项目的最简单方法是将点击事件标记为Handled,如果ClickCount 小于2

    这也可以让您保留您的Trigger,当它被选中时将文本设置为Bold

    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <EventSetter Event="PreviewMouseDown" Handler="ListBoxItem_PreviewMouseDown" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    
    
    private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount < 2)
            e.Handled = true;
    }
    

    请记住,这会禁用ListBoxItem 上的所有单击事件。如果您想允许某些单击事件,则必须调整 PreviewMouseDown 事件以不将特定点击标记为 Handled

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2017-05-21
      • 1970-01-01
      相关资源
      最近更新 更多