如何禁用Windows Phone 8.1 ListView中某些项目的选择

时间:2015-09-16 08:49:37

标签: c# windows-phone-8.1 windows-8.1

我的Windows Phone 8.1中有一个ListView,可以选择多个项目。一些项目不可选择。我为这两种项目定义了一个DataTemplate选择器。我用于绑定的类具有IsSelectable属性,用于确定要使用哪个DataTemplate

<ListView SelectionMode="Multiple">
<ListView.ItemTemplate>
<DataTemplate x:Key="KeyValueDataTemplate">
        <utils:KeyValueTemplateSelector HorizontalAlignment="Stretch" Content="{Binding}">
            <utils:KeyValueTemplateSelector.NotSelectable>
                <DataTemplate>
                    <!-- some controls -->
                </DataTemplate>
            </utils:KeyValueTemplateSelector.NotSelectable>
            <utils:KeyValueTemplateSelector.Selectable>
                <DataTemplate>
                    <!-- some controls -->
                </DataTemplate>
            </utils:KeyValueTemplateSelector.Selectable>
        </utils:KeyValueTemplateSelector>
    </DataTemplate>
</ListView.ItemTemplate>
</ListView>

使用SelectionMode="Multiple"选项会自动在每个项目旁边显示CheckBox。我需要为不可选择的项隐藏CheckBox。我尝试修改ListViewItem Style并绑定复选框Visibility IsSelectable(使用BooleanToVisibilityConverter)。但它似乎没有用。

1 个答案:

答案 0 :(得分:0)

在DataTemplate的任何部分设置此属性:

local:SelectorHelper.IsItemSelectionEnabled="{Binding IsSelectable}"

示例:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid local:SelectorHelper.IsItemSelectionEnabled="{Binding IsSelectable}">
            <!-- -->
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

这是我的SelectorHelper:

public static class SelectorHelper
{
    public static readonly DependencyProperty IsItemSelectionEnabledProperty =
        DependencyProperty.RegisterAttached("IsItemSelectionEnabled", typeof(bool), typeof(SelectorHelper), new PropertyMetadata(true, IsItemSelectionEnabledChanged));


    public static void SetIsItemSelectionEnabled(DependencyObject dependencyObject, bool value)
    {
        dependencyObject.SetValue(IsItemSelectionEnabledProperty, value);
    }

    public static bool GetIsItemSelectionEnabled(DependencyObject dependencyObject)
    {
        return (bool)dependencyObject.GetValue(IsItemSelectionEnabledProperty);
    }


    private static void IsItemSelectionEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = (FrameworkElement)d;
        var selectorItem = XamlTreeHelper.FindParent<SelectorItem>(frameworkElement);
        if (selectorItem != null)
        {
            selectorItem.IsEnabled = GetIsItemSelectionEnabled(frameworkElement);
        }
        else
        {
            frameworkElement.Loaded -= OnSelectableItemLoaded;
            frameworkElement.Loaded += OnSelectableItemLoaded;
        }
    }

    private static void OnSelectableItemLoaded(object sender, RoutedEventArgs e)
    {
        var frameworkElement = (FrameworkElement)sender;
        var selectorItem = FindParent<SelectorItem>(frameworkElement);
        if (selectorItem != null)
        {
            selectorItem.IsEnabled = GetIsItemSelectionEnabled(frameworkElement);
        }
    }

    private static T FindParent<T>(DependencyObject child) where T : DependencyObject
    {
        var currentObject = child;
        do
        {
            currentObject = VisualTreeHelper.GetParent(currentObject);
            var parent = currentObject as T;
            if (parent != null)
            {
                return parent;
            }
        } while (currentObject != null);

        return null;
    }
}