如何禁止交互式列表框项的选择? [英] How to disallow the selection of an interactive ListBox item?

查看:123
本文介绍了如何禁止交互式列表框项的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些列表框我使用包含一个按钮和一个TextBox的模板项目。我怎样才能使它使得它不可能从列表中选择这些项目,但仍然可以用按钮来interract?

Some of the items in my ListBox use a template that contains a button and a TextBox. How can I make it such that it is impossible to select these items from the list, but still possible to interract with the button?

编辑:

我还需要能够选择在该列表中的其他项目,只是没有那些与此模板。

I still need to be able to select other items in this list, just not ones with this template.

推荐答案

我们可以使用一个附加属性的ListBoxItem的(我实施后,我发现<一个href=\"http://$c$crelief.net/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/\"相对=nofollow>人谁做了几乎相同):

We can use a attached property to the ListBoxItem (after I implemented I found someone who had done almost the same) :

public class ListBoxItemEx
{
    public static bool GetCanSelect(DependencyObject obj)
    {
        return (bool)obj.GetValue(CanSelectProperty);
    }
    public static void SetCanSelect(DependencyObject obj, bool value)
    {
        obj.SetValue(CanSelectProperty, value);
    }
    public static readonly DependencyProperty CanSelectProperty =
        DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(ListBoxItemEx), new UIPropertyMetadata(true, OnCanSelectChanged));

    private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var item = sender as ListBoxItem;
        if (item == null)
            return;

        if ((bool)args.NewValue)
        {
            item.Selected -= ListBoxItemSelected;
        }
        else
        {
            item.Selected += ListBoxItemSelected;
            item.IsSelected = false;
        }
    }

    private static void ListBoxItemSelected(object sender, RoutedEventArgs e)
    {
        var item = sender as ListBoxItem;
        if (item == null)
            return;
        item.IsSelected = false;
    }
}

在XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="525"
    Height="350">
<Window.Resources>
    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="local:ListBoxItemEx.CanSelect" Value="{Binding CanSelect}"/>
    </Style>
</Window.Resources>     
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid Name="LayoutRoot">
    <ListBox ItemsSource="{Binding Elements}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}" SelectionMode="Multiple" />
</Grid>

视图模型:

public class ViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged values

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion


    public List<Dummy> Elements { get; set; }

    public ViewModel()
    {
        this.Elements = new List<Dummy>(){
            new Dummy() { CanSelect =true, MyProperty = "Element1"},
            new Dummy() { CanSelect =false, MyProperty = "Element2"},
            new Dummy() { CanSelect =true, MyProperty = "Element3"},
            new Dummy() { CanSelect =false, MyProperty = "Element4"},
            new Dummy() { CanSelect =true, MyProperty = "Element5"},
            new Dummy() { CanSelect =true, MyProperty = "Element6"},
            new Dummy() { CanSelect =true, MyProperty = "Element7"},
            new Dummy() { CanSelect =true, MyProperty = "Element8"},
            new Dummy() { CanSelect =false, MyProperty = "Element9"},
        };
    }
}

public class Dummy
{
    public bool CanSelect { get; set; }

    public string MyProperty { get; set; }

    public override string ToString()
    {
        return this.MyProperty;
    }
}

这种方法唯一需要注意的是,试图选择一个不可选的项目取消选择当前选中的一个,如果ListBox中有一个选择,或者未选中所有,如果列表框已扩展选择。

The only caveat with this approach is that trying to select an unselectable item unselects the current selected one if the ListBox has single selection, or unselects all, if the ListBox has extended selection.

这篇关于如何禁止交互式列表框项的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆