如何禁用listview windows phone中的某些项目选择

时间:2014-03-09 19:28:47

标签: c# windows-phone listboxitem

我想禁用选项 listBoxEmployee 整个 isEnable 属性= false但是所有这些项目仍然可以选择,我不知道为什么。在我的主页中,我的代码是:

public SelectLevel()
        {
            InitializeComponent();

            passedLevel = (int)IsolatedStorageSettings.ApplicationSettings["passedLevel"];

            List<LevelList> myData = new List<LevelList>();

            for (int i = 0; i <= passedLevel; i++)
            {
                myData.Add(new LevelList { levelNumber = i, urlImg = "Images/passed.png", IsEnabled = true});
            }

            for (int j = passedLevel + 1; j <= allLevel; j++)
            {
                myData.Add(new LevelList { levelNumber = j, urlImg = "Images/notpassed.png", IsEnabled = false });

            }

            listBoxEmployee.ItemsSource = myData;

        }

我的xaml页面:

<ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <Grid Width="80" Height="80" IsHitTestVisible="{Binding IsEnabled}">
                                <Image Source="{Binding urlImg}" IsHitTestVisible="{Binding IsEnabled}"></Image>
                                <TextBlock Text="{Binding IsEnabled}" IsHitTestVisible="{Binding IsEnabled}" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="36" Foreground="Black" />

                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

2 个答案:

答案 0 :(得分:2)

您真正要做的是在列表框项目中的控件上设置IsHitTestVisible,而不是列表框项目本身。点击事件会冒泡到ListBoxItem并允许其被选中。我认为你可以通过覆盖ListBoxItemListBox的风格来做你想做的事。从列表框项样式的副本开始,然后添加Setter以更改IsHitTestVisible属性。例如:

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}"></ListBox>

在您的页面资源中(仅从副本更改为IsHitTestVisible属性):

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
        <Setter Property="IsHitTestVisible" Value="{Binding IsEnabled}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

答案 1 :(得分:1)

我怀疑问题是由@PeterRitchie解释的。如果这是正确的,我认为有一种更简单的方法来解决它。尝试使用样式设置器以这种方式绑定IsHitTestVisible ListBoxItem属性:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsHitTestVisible" Value="{Binding IsEnabled}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>