精华内容
下载资源
问答
  • WPF ItemsControl ListBox ListView比较 原文:WPF ItemsControl ListBox ListView比较在进行列表信息展示时,WPF中提供多种列表可供选择。这篇博客将对WPF ItemsControl, ListBox, ListView进行比较。...
    原文:WPF ItemsControl ListBox ListView比较

    在进行列表信息展示时,WPF中提供多种列表可供选择。这篇博客将对WPF ItemsControl, ListBox, ListView进行比较。

    相同点:

    1. 这三个控件都是列表型控件,可以进行列表绑定(ItemsSource);

    2. 这三个控件均使用ItemsPresenter来展示列表信息;

    不同点:

    控件层次关系:

    ItemsControl:

    System.Object
      System.Windows.Threading.DispatcherObject
        System.Windows.DependencyObject
          System.Windows.Media.Visual
            System.Windows.UIElement
              System.Windows.FrameworkElement
                System.Windows.Controls.Control
                  System.Windows.Controls.ItemsControl

    ListBox:

    System.Object
      System.Windows.Threading.DispatcherObject
        System.Windows.DependencyObject
          System.Windows.Media.Visual
            System.Windows.UIElement
              System.Windows.FrameworkElement
                System.Windows.Controls.Control
                  System.Windows.Controls.ItemsControl
                    System.Windows.Controls.Primitives.Selector
                      System.Windows.Controls.ListBox

    ListBox 继承于ItemsControl,增加了一个Selector对象,ItemsControl中的Item是不支持选择的。而ListBox中Item是支持选择,并且可以单选,多选。

    ListView:

    System.Object
      System.Windows.Threading.DispatcherObject
        System.Windows.DependencyObject
          System.Windows.Media.Visual
            System.Windows.UIElement
              System.Windows.FrameworkElement
                System.Windows.Controls.Control
                  System.Windows.Controls.ItemsControl
                    System.Windows.Controls.Primitives.Selector
                      System.Windows.Controls.ListBox
                        System.Windows.Controls.ListView

    ListView继承与ListBox,增加了一个View依赖属性。

    ItemsControl是不包含水平和垂直方向的滚动条的。ListBox和ListView有水平和垂直方向滚动条。

    ItemControl的样式:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style x:Key="ItemsControlDefaultStyle" TargetType="{x:Type ItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
    
    Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- Resource dictionary entries should be defined here. -->
    </ResourceDictionary>

    ListBox和ListView的样式基本一样,除了TargetType外,

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="ListBorder" Color="#828790"/>
        <Style x:Key="ListBoxDefaultStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" 
    
    SnapsToDevicePixels="true">
                            <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </ScrollViewer>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsGrouping" Value="true">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- Resource dictionary entries should be defined here. -->
    </ResourceDictionary>

    在项目中如何选择使用这三个控件;

    1. 如果列表信息只做展示,但不提供选择功能,可以使用ItemsControl;

    2. ListView比ListBox增加了一个View属性。

    示例代码:

    ItemsControl vs ListBox (Selector)

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            
            <!--ItemsControl-->
            <StackPanel>
                <TextBlock Text="ItemsControl" FontSize="18"/>
    
                <ItemsControl ItemsSource="{Binding .}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Ellipse Width="110" Height="55" Fill="#ebebee"/>
    
                                <StackPanel>
                                    <TextBlock Text="{Binding Priority}" FontSize="16" HorizontalAlignment="Center"/>
                                    <TextBlock Text="{Binding Name}" FontSize="16" HorizontalAlignment="Center"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
    
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
    
                    <ItemsControl.ItemContainerStyle>
                        <Style>
                            <Setter Property="Control.Margin" Value="5"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>
            </StackPanel>
    
            <!--ListBox-->
            <StackPanel Grid.Row="1">
                <TextBlock Text="ListBox" FontSize="18"/>
                <ListBox ItemsSource="{Binding .}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Ellipse Width="110" Height="55" Fill="#ebebee"/>
    
                                <StackPanel>
                                    <TextBlock Text="{Binding Priority}" FontSize="16" HorizontalAlignment="Center"/>
                                    <TextBlock Text="{Binding Name}" FontSize="16" HorizontalAlignment="Center"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
    
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
    
                    <ListBox.ItemContainerStyle>
                        <Style>
                            <Setter Property="Control.Width" Value="120"/>
                            <Setter Property="Control.Margin" Value="5"/>
                        </Style>
                    </ListBox.ItemContainerStyle>
    
                    <ListBox.Template>
                        <ControlTemplate>
                            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                                <ItemsPresenter/>
                            </ScrollViewer>
                        </ControlTemplate>
                    </ListBox.Template>
                </ListBox>
            </StackPanel>
        </Grid>

    C#

        public class Task
        {
            public string Name { get; set; }
    
            public int Priority { get; set; }
        }
    
        ObservableCollection<Task> _tasks = null;
        public MainWindow()
        {
            InitializeComponent();
    
            _tasks = new ObservableCollection<Task>()
            {
                new Task() { Name = "Shopping",Priority = 2 },
                new Task() { Name = "Laundry",Priority = 2 },
                new Task() { Name = "Email",Priority = 1 },
                new Task() { Name = "Writting",Priority = 2 },
                new Task() { Name = "Learning",Priority = 2 },
                new Task() { Name = "Working",Priority = 2 },
            };
    
            DataContext = _tasks;
        }

    运行效果:

    ListView View属性的使用

    <ListView ItemsSource="{Binding .}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Task Name" DisplayMemberBinding="{Binding Name}" Width="100"/>
                    <GridViewColumn Header="Task Priority" DisplayMemberBinding="{Binding Priority}" Width="100"/>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

    运行效果:

    感谢您的阅读,代码点击这里下载。

    posted on 2018-11-12 14:44 NET未来之路 阅读(...) 评论(...) 编辑 收藏

    转载于:https://www.cnblogs.com/lonelyxmas/p/9946388.html

    展开全文
  • WPF listBox listview去掉滚动条

    千次阅读 2013-09-04 16:07:13
    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Visible"></ListBox> <ListBox ScrollViewer.VerticalScrollBarVisibility="Visible"></ListBox> 或

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Visible"></ListBox>

    <ListBox ScrollViewer.VerticalScrollBarVisibility="Visible"></ListBox>

    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
    < Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>

    展开全文
  • ListBoxListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现。 一.重点  对于ListBoxListView来讲,后台绑定的ItemSource绑定的一般...

     

     ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现。

    一.重点

      对于ListBox和ListView来讲,后台绑定的ItemSource绑定的一般都是List<T>格式,而List<T>有个方法是Take和Skip,分别意思是取List部分和跳过List部分。

    取数据的格式是:List.take().Skip();

     

    二.话不多说,实例说话(后面会附有该例子链接,仅供参考)

    (1)Xaml界面上:

    <StackPanel>
            <ListBox Height="300"  ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding ListShow}"></ListBox>
            <Button x:Name="TurnUpButton" Content="上翻页" Click="TurnPageUp"   Height="50" />
            <Button x:Name="TurnDownButton" Content="下翻页" Click="TurnPageDown" Height="50"/>
            <Button Content="自动翻页" Click="Button_Click_1" Height="50"/>
        </StackPanel>

    ListBox是数据显示区域,三个Button的Content已写明各自功能。

    (2)CS代码

     public partial class MainWindow : Window
        {
    
            public ObservableCollection<string> ListShow
            {
                get { return (ObservableCollection<string>)GetValue(ListShowProperty); }
                set { SetValue(ListShowProperty, value); }
            }
    
            public List<string> NameList = new List<string>();
    
            public MainWindow()
            {
                InitializeComponent();
                //WPF数据绑定
                this.DataContext = this;
                //假数据创建
                Data();
                //数据显示
                ContentShow();
                //Timer初始化 为自动翻页做准备
                TimeSet();
               
            }
    
            private void Data()
            {
                for (int i = 0; i < 30; i++)
                {
                    string s = "Name" + i.ToString();
                    NameList.Add(s);
                }
            }
    
            private void ContentShow()
            {
                ListShow = new ObservableCollection<string>(NameList.Take(PageSize * Page).Skip(PageSize * (Page - 1)));
                if (NameList.Count % PageSize == 0)
                {
                    Total = NameList.Count / PageSize;
                }
                else
                {
                    Total = NameList.Count / PageSize + 1;
                }
                TurnUpButton.Visibility = Page > 1 ? Visibility.Visible : Visibility.Hidden;
                TurnDownButton.Visibility = Page < Total ? Visibility.Visible : Visibility.Hidden;
            }
    
            private void TimeSet()
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(5);
                timer.Tick += timer_Tick;
            }
    
            void timer_Tick(object sender, EventArgs e)
            {
                if (Page < Total)
                {
                    Page++;
                    ContentShow();
                }
                else
                {
                    Page = 1;
                    ContentShow();
                }
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                if (IsOpen == true)
                {
                    IsOpen = false;
                    timer.Stop();
                }
                else
                {
                    timer.Start();
                    IsOpen = true;
                }
            }
    
            private void TurnPageUp(object sender, RoutedEventArgs e)
            {
                Page--;
                ContentShow();
            }
    
            private void TurnPageDown(object sender, RoutedEventArgs e)
            {
                Page++;
                ContentShow();
            }
            DispatcherTimer timer;
            private bool IsOpen;
            private int Page = 1;
            private int Total;
            private int PageSize = 7;
    
            public static readonly DependencyProperty ListShowProperty =
                DependencyProperty.Register("ListShow", typeof(ObservableCollection<string>), typeof(MainWindow), new PropertyMetadata(new ObservableCollection<string>()));
    
        }

      ListShow是Xaml的ListBox的ItemSource绑定对象,所有数据都是通过ListShow显示到界面上的。

      强调一定要用ObservableCollection<T>类型,这样才能在ListShow数据发生改变后,界面自动刷新,普通List<T>赋值上去是不会刷新界面的。

      Page--当前页数。PageSize--每页数据数量。Total--总页数。

                ListShow = new ObservableCollection<string>(NameList.Take(PageSize * Page).Skip(PageSize * (Page - 1)));

      Take取PageSize*Page条数据,Skip跳过(PageSize*(Page-1))条数据,实际获取的就是1页的数据,即1个PageSize。

     

      下面两条代码是为了限制在Page在首页和尾页时,不能继续向前翻和后翻,而设置的可见不可见。

      TurnUpButton.Visibility = Page > 1 ? Visibility.Visible : Visibility.Hidden;
                TurnDownButton.Visibility = Page < Total ? Visibility.Visible : Visibility.Hidden;

     

     

    三.易错点

    (1)timer一定要用DispatcherTimer,如果只是用Timer会因为线程问题而不能实现自动翻页。

    (2)依赖属性的Register中最后面的New PropertyMedata中是new出来一个空的ObservableCollection<string>(),不是String.Empty或者Null

     

    四.代码下载

      

    转载于:https://www.cnblogs.com/Khan-Sadas/p/4931018.html

    展开全文
  • 适用ListBox /ListView WPFLISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html 转载于:https://www.cnblogs.com/wgscd/p/10268104.html

    适用ListBox /ListView

    WPF中LISTVIEW如何改变选中条背景颜色

    https://www.cnblogs.com/sjqq/p/7828119.html

    转载于:https://www.cnblogs.com/wgscd/p/10268104.html

    展开全文
  • WPF 自定义ItemsControl/ListBox/ListView控件样式 原文:WPF 自定义ItemsControl/ListBox/ListView控件样式一、前言 ItemsControl、ListBoxListView这三种控件在WPF中都可作为列表信息展示控件。...
  • listBoxlistView实现不同的效果...
  • private void Button1_Click(object sender, RoutedEventArgs e) { var curItem = ((ListBoxItem)lvQueue.ContainerFromElement((Button)sender)).Content; DataRowView drv = c...
  • ListView vs ListBox performance in WPF

    千次阅读 2010-02-26 11:32:00
    Ive been listing a relatively large number of items in a ListView (8000+) and it is great in terms of performance, fills in less than a second.Ive tried the same with a ListBox and it takes a lot mo
  • wpf修改ListViewListBox风格,介绍了在wpfListViewListBox的使用
  • 今天碰到一个需求,使用ListBox显示多文字。因为每个选项文字较长,ListBoxItem的宽度有限,这时候为了体现界面友好,增加ToolTip是一个好的选择:代码如下: MainWindow.xmal: <Window x:Class="_20200116_...
  • ListView Name="BoardListView" ScrollViewer.VerticalScrollBarVisibility="Hidden" Height="100" VerticalAlignment="Bottom"> <ListView.ItemsPanel> <ItemsPanelTemplate&...
  • WPF技巧20190722 LIstbox/ListView 自动流动到数据最后一行一.需求:二. 实现方式1. 实现方式一1.1 绑定1.2 自动滚动1.2.1 ViewModel中定义更改列表方法:1.2.2 在主界面后台代码中增加自动滚动条方法:2 实现方式二:...
  • WPF ListViewListBox(选中)项样式

    千次阅读 2020-02-05 14:40:40
    WPF ListViewListBox(选中)项自定义样式 想用ListBox或者ListView控件,做一个菜单栏等,却发现很难改变ListBoxItem/ListViewItem项在鼠标移过、选中的默认蓝色背景与边框。 首先尝试使用控件的Style来设置...
  • 原文:WPFListViewListBox中的WrapPanel 自动换行在ListView或者ListBox中使用WrapPanel想让ItemTemplate中的内容像下图这样先横向平铺然后再纵向换行,默认设置是不能实现的。图1. 横向平铺再纵向换行的效果示例...
  • 关于WPF的资料极少,在使用的期间遇到过很多... 最近在使用ListBoxListViewListBox了个View,问题是一样的)的时候,发现ListBox相对其他控件而言比较特别,用设置其他控件的方式去设设置Trigger都无法改变TextB...
  • <ListBox.ItemTemplate> " {Binding} " > </ListBox.ItemTemplate> </ListBox> <ListBox x:Name= " mb2 " Height= " 100 " > <ListBox.ItemTemplate> " {Binding} " > </ListBox.ItemTemplate> ...
  • WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景:  需要检索某目录下文件,并列出来,提供选择和其他功能。 第一版需求:  列出文件供选择即可,代码如下: <ListBox ...

空空如也

空空如也

1 2 3 4 5 ... 10
收藏数 189
精华内容 75
关键字:

listboxlistviewwpf