【问题标题】:Disable list box selection in D&D在 D&D 中禁用列表框选择
【发布时间】:2014-01-12 08:31:50
【问题描述】:

我正在使用列表框,当您单击其上的项目时,您会看到蓝线标记在列表框的值上,我想禁用它,您无法标记列表框的值并且 复制粘贴

我尝试了 IsSelected="False" 但没有成功...

<ListBox x:Name="seus" IsSelected="False"  Height="115" Width="150" ItemsSource="{Binding Use}" SelectionChanged="listbox_SelectionChanged" AllowDrop="True" PreviewDrop="ListBox_PreviewDrop" />

这是列表框的事件

private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (sender is ListBox)
    {
        var listBox = sender as ListBox;
        if (e.AddedItems.Count == 1)
        {
            if (listBox.SelectedItem != null)
            {
                var mySelectedItem = listBox.SelectedItem as User;
                if (mySelectedItem != null)
                {
                    DragDrop.DoDragDrop(listBox, mySelectedItem.Name, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }
    }
    else
        return;
}

【问题讨论】:

  • 就在字里行间,否则返回;在这种情况下是一个冗余的控制流跳转语句。你可以简单地删除它。

标签: c# wpf xaml mvvm listbox


【解决方案1】:

关闭选择确实有点棘手。但是添加一个 ItemContainerStyle 就可以了:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border>
                        <TextBlock HorizontalAlignment="Stretch" Text="{Binding}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

我知道这只是成功的一半。它不会关闭复制/粘贴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多