【问题标题】:How to disable Virtualizing for lisbox item Wp8如何禁用列表框项目 Wp8 的虚拟化
【发布时间】:2015-02-12 10:27:11
【问题描述】:

我无法为列表框禁用 VirtualizingStackPanel 下的 Virtualizing 属性。我只获得 VirtualizationMode 属性。 实际上我需要在列表框下获取复选框元素。尝试了几种方法,但最合适的是我得到了here 但我将 ItemContainerGenerator 设为空。 经过更多 rnd 我发现我需要设置 IsVirtualizing=false 才能获得 ItemContainerGenerator 。 xml 是 ::

<ListBox x:Name="my_list" Grid.Row="0">
            <ItemsControl.ItemTemplate >
                <DataTemplate >
                    <StackPanel Orientation="Horizontal" >
                        <CheckBox x:Name="cbx_state"  Tag="{Binding}"/>
                        <TextBlock x:Name="txt_string" Text="{Binding}" VerticalAlignment="Center" FontSize="34" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

我无法获取 VirtualizingStackPanel.isvertualing 属性。我试图获取 cbx_state。

【问题讨论】:

  • 您的问题是您必须从 c# 代码中选中复选框,对吗?
  • 是尝试了一种方法.....但是在使用它时 ItemContainerGenerator 作为空值,我发现这是由于 isvertualing 属性设置为 true...我必须将其设置为 false获取 ItemContainerGenerator
  • here 它说要设置 VirtualizingStackPanel.isvertualing=false 我无法获得 .isvertualing 属性

标签: windows-phone-8 checkbox listbox


【解决方案1】:

我以这种方式完成了我从 c# 中选中的复选框 在 Page1.xaml 中:

 <phone:PhoneApplicationPage
x:Class="PhoneApp3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid Background="AliceBlue">
<ListBox x:Name="my_list" Height="200" Grid.Row="0">
    <ItemsControl.ItemTemplate >
        <DataTemplate >
            <StackPanel Orientation="Horizontal" >
                <CheckBox x:Name="cbx_state"  Tag="{Binding BindsDirectlyToSource=True}"/>
                    <TextBlock x:Name="txt_string" Text="{Binding BindsDirectlyToSource=True}" VerticalAlignment="Center" FontSize="34" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>
</Grid>

在 page1.cs 中

namespace PhoneApp3
{
public partial class Page1 : PhoneApplicationPage
{
    public Page1()
    {
        InitializeComponent();
        String[] names = { "Virat", "Rohit", "Rahane", "Umesh", "Axar" };
        my_list.ItemsSource = names;
        this.Loaded += Page1_Loaded;
    }

    void Page1_Loaded(object sender, RoutedEventArgs e)
    {
        GetItemsRecursive(my_list);
    }

    private void GetItemsRecursive(DependencyObject lb)
    {
        var childrenCount = VisualTreeHelper.GetChildrenCount(lb);

        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(lb, i);

            if (child is CheckBox) // specific/child control 
            {
                CheckBox targeted_element = (CheckBox)child;

                targeted_element.IsChecked = true;

                if (targeted_element.IsChecked == true)
                {

                    return;
                }
            }

            GetItemsRecursive(child);
        }
    }
}
 }

【讨论】:

  • 非常感谢@Arun.P 它可以按我的意愿工作......我从几周开始就一直在尝试,非常感谢。 DependencyObject child = VisualTreeHelper.GetChild(lb, i);而不是 var child
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2021-02-03
相关资源
最近更新 更多