wpf开发控件多选实现

1 怎样把TreeView中所以节点全部展开?

  不同于Windows Forms,当前WPF版本没有提供一个直接的方法可以把TreeView控件所有的节点都展开。一般来说,在WPF中有两种方法可以实现这个功能。第一种方法就像下面例子一样使用样式展开所有节点:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Page.Resources>

        <XmlDataProvider x:Key="treeData" XPath="*">

            <x:XData>

                <Items Name="Items" xmlns="">

                    <Item1/>

                    <Item2>

                        <Item22/>

                        <Item12/>

                        <Item13>

                            <Item131/>

                            <Item131/>

                        </Item13>

                    </Item2>

                </Items>

            </x:XData>

        </XmlDataProvider>

        <HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}"

                             x:Key="template">

            <TextBlock Name="textBlock" Text="{Binding Name}"/>

    </HierarchicalDataTemplate>

    </Page.Resources>

    <TreeView ItemTemplate="{StaticResource template}"

           ItemsSource="{Binding Source={StaticResource treeData}}">

        <TreeView.ItemContainerStyle>

            <!--Using style setter to set the TreeViewItem.IsExpanded property to true, this will be applied

      to all TreeViweItems when they are generated-->

            <Style TargetType="{x:Type TreeViewItem}">

                <Setter Property="IsExpanded" Value="True"/>

            </Style>

        </TreeView.ItemContainerStyle>

    </TreeView>

</Page>

  有些时候,你需要自己写代码去实现这个功能,下面的是用C#代码写成的一个帮助类可以展开TreeView控件中所有的节点:

  public static class TreeViewHelper

  {

        public static void ExpandAll(TreeView treeView)

        {

            ExpandSubContainers(treeView);

        }

        private static void ExpandSubContainers(ItemsControl parentContainer)

        {

            foreach (Object item in parentContainer.Items)

            {

                TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                if (currentContainer != null && currentContainer.Items.Count > 0)

                {

                    //展开当前节点

                    currentContainer.IsExpanded = true;

                    if (currentContainer.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)

                    {

                        //如果装入当前节点的容器还没有生成好,我们应该等待到它一直生成完成。

                        currentContainer.ItemContainerGenerator.StatusChanged += delegate

                        {

                            ExpandSubContainers(currentContainer);

                        };

                    }

                    else

                    {

                        //如果装入当前节点的容器已经生成,我们可以直接展开它。

                        ExpandSubContainers(currentContainer);

                    }

                }

            }

        }

}

  上面这个例子的关键之处就是要确认当前的TreeViewItem的容器已经生成了,所有你可以安全的展开当前节点的所有的子节点。这就是为什么你需要采用递归的方法(当前的节点ItemContainerGenerator状态是GeneratorStatus.ContainersGenerated.时)。

  2 怎样在ListBox/ListView中实现多项选择?

  (Lasso selection)在图形编辑程序中用得非常普遍,这种方法可以方便的选中屏幕上的多个对象。下面的这个例子展示了针对WPF的控件ListBox/ListView实现这个功能。

<ListBox Name="listBox"

        Width="200"

        Height="200"

        SelectionMode="Multiple">

        <ListBox.Resources>

            <Style TargetType="{x:Type ListBoxItem}">

                <EventSetter Event="ListBoxItem.PreviewMouseLeftButtonDown"

                  Handler="ListBoxItem_PreviewMouseLeftButtonDown"/>

                <EventSetter Event="ListBoxItem.PreviewMouseUp"

                  Handler="ListBoxItem_PreviewMouseUp"/>

                <EventSetter Event="ListBoxItem.PreviewMouseMove"

                  Handler="ListBoxItem_PreviewMouseMove"/>

            </Style>

        </ListBox.Resources>

        <x:Type TypeName="DependencyObject"/>

        <x:Type TypeName="Visual"/>

        <x:Type TypeName="UIElement"/>

        <x:Type TypeName="FrameworkElement"/>

        <x:Type TypeName="Control"/>

    </ListBox>

 

public partial class Window1 : Window

    {

        //这个字段用于显示ListBox是否是鼠标选择状态

        private Boolean inMouseSelectionMode = false;

        //这个字段用于与当前鼠标选择的项保持联系

        private List<ListBoxItem> selectedItems = new List<ListBoxItem>();

        public Window1()

        {

            InitializeComponent();

        }

        private void ListBoxItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)

        {

            //若鼠标松开,则关闭"inMouseSelectionMode"

            inMouseSelectionMode = false;

        }

        private void ListBoxItem_PreviewMouseMove(object sender, MouseEventArgs e)

        {

            ListBoxItem mouseOverItem = sender as ListBoxItem;

            if (mouseOverItem != null && inMouseSelectionMode && e.LeftButton ==MouseButtonState.Pressed)

            {

                mouseOverItem.Background = SystemColors.HighlightBrush;

                //高亮显示当前位于鼠标下方的项

                mouseOverItem.SetValue(TextElement.ForegroundProperty,SystemColors.HighlightTextBrush);

                if (!selectedItems.Contains(mouseOverItem))

                {

                    selectedItems.Add(mouseOverItem);

                }

            }

        }

        private void ListBoxItem_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)

        {

            //当鼠标点击时,我们需要消除所有先前选中的项

            listBox.SelectedIndex = -1;

            inMouseSelectionMode = true;

            foreach (ListBoxItem item in selectedItems)

            {

                item.ClearValue(ListBoxItem.BackgroundProperty);

                item.ClearValue(TextElement.ForegroundProperty);

            }

            selectedItems.Clear();

        }

}

  3 怎样实现单选列表控件?

  WPF没有像ASP.NET提供一个RadioButtonList的控件。幸运的是,我们可以利用WPF样式和模板的强大功能,用纯粹的XAML代码实现这个功能,下面是一个在XAMLPad可运行的例子:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

     xmlns:s="clr-namespace:System;assembly=mscorlib">

    <Page.Resources>

        <x:Array Type="{x:Type s:String}" x:Key="data">

            <s:String>Option1</s:String>

            <s:String>Option2</s:String>

            <s:String>Option3</s:String>

        </x:Array>

    </Page.Resources>

    <StackPanel DataContext="{StaticResource data}">

        <TextBlock Margin="5">

      <TextBlock Text="Current Option:"/>

      <TextBlock Text="{Binding /}"/>

    </TextBlock>

        <ListBox

       ItemsSource="{Binding}"

       IsSynchronizedWithCurrentItem="True"

       Width="240"

       Height="60"

       HorizontalAlignment="Left">

            <ListBox.ItemContainerStyle>

                <Style TargetType="{x:Type ListBoxItem}">

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type ListBoxItem}">

                                <RadioButton

                   IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSourceTemplatedParent}}"

                   Content="{TemplateBinding Content}"/>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.ItemContainerStyle>

        </ListBox>

    </StackPanel>

</Page>

 原文地址:

http://silverlightchina.net/html/study/WPF/2012/0904/18642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF Combobox是一个常见的下拉列表件,它能够快速地展示和择不同的项。在某些情况下,我们需要实现 Combobox 的多功能,以便用户能够择多个项。本文将为您详细介绍如何使用 WPF Combobox 实现实现的一种简单的方式是将 WPF Combobox 显示为列表框(ListBox),然后允许用户在列表中择多个项。要实现这样的多功能,需要使用 ItemsControl 和 ListBox 件。首先,使用ItemsControl将要用于展示的项集合附加到 Combobox 上。 ```xaml <!--定义ItemsControl--> <ItemsControl ItemsSource="{Binding Selection}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" Margin="5" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> ``` 其中,`Selection`是项的集合。接下来,将 Combobox 的模板更改为 ListBox,使其在下拉列表中显示所有项。 ```xaml <!--定义Combobox--> <ComboBox x:Name="ComboBox"> <ComboBox.Template> <ControlTemplate TargetType="ComboBox"> <Grid> <ToggleButton x:Name="ToggleButton" Content="Click" /> <Popup x:Name="Popup"> <ListBox ItemsSource="{Binding Selection}" SelectionMode="Multiple" /> </Popup> </Grid> </ControlTemplate> </ComboBox.Template> </ComboBox> ``` 在 Combobox 的模板中,使用 ToggleButton 将 Popup 隐藏和显示。Popup 中包含 ListBox,且设置 SelectionMode 属性为 Multiple,使其能够择多个项。此时,每个项在 Popup 中显示为 ListBox 中的一个项,而不是 Combobox 的下拉列表中的一个项。 在编写代码时,需要先将 Combobox 显示为 ListBox,然后使用 Popup 特性将其隐藏。此外,还需要为 ListBox 添加事件处理程序,以便在用户项时更新组合框的项集合。 ```c# //实现 ComboBox.IsEditable = true; var itemsControl = ((ComboBox)ComboBox).Template.FindName("PART_ItemList", ComboBox) as ItemsControl; var listBox = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ListBox; if (listBox != null) { listBox.SelectionMode = SelectionMode.Multiple; listBox.SelectionChanged += listBox_SelectionChanged; } //更新项 var listBox = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ListBox; if (listBox != null) { listBox.SelectionChanged -= listBox_SelectionChanged; for (int i = 0; i < listBox.SelectedItems.Count; ++i) { Selection.Add(listBox.SelectedItems[i].ToString()); } listBox.SelectionChanged += listBox_SelectionChanged; } ``` 最后,用户择多个项时,会在 Selection 集合中添加项,当用户关闭 Popup 时,将更新项的 Text 和 Popup 的 IsOpen 属性。 如上所述,这是使用 WPF Combobox 实现的一种简单方法。我们先将 Combobox 显示为 ListBox,再使用 Popup 特性将Popup隐藏,然后添加事件处理程序以更新项集合。这种方式虽然看起来有点繁琐,但要实现需要的代码量很少,而且在使用上比一些自定义件的方式更灵活简便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值