wpf 中ListBox的用法

时间:2022-04-07 15:49:09

   ListBox可以用来显示类似web中新闻列表的样式,显示也比较灵活,可以自定定义模板

        <DataTemplate x:Key="listBoxTemplate">
            <StackPanel Margin="4">
                <DockPanel>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition  />
                            <RowDefinition  Height="2" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition  Width="500" />
                            <ColumnDefinition  />
                        </Grid.ColumnDefinitions>
                        <Image Height="19" Source="/Financial;component/Images/main/新闻列表图标.png" Grid.Row="0" Grid.Column="0" 
Name
="image1" Stretch="Fill" VerticalAlignment="Top" Width="19" /> <TextBlock Text="{Binding Title}" Grid.Row="0" Grid.Column="1" Foreground="White" /> <TextBlock Text="{Binding AddTime,Mode=OneWay,StringFormat='yyyy-MM-dd HH:mm:ss'}" Grid.Row="0" Grid.Column="2" Foreground="White"/> <Image Height="2" Source="/Financial;component/Images/main/新闻列表下划线.png" Grid.Row="1" Grid.ColumnSpan="3" Margin="0,0" Name="image2"
Stretch
="Fill" VerticalAlignment="Top" Width="710" /> </Grid> </DockPanel> </StackPanel> </DataTemplate> </UserControl.Resources> <Grid Name="gdMainPanel" Width="725" Height="260" Margin="5,0"> <TextBlock Height="23" HorizontalAlignment="Left" Margin="114,18,0,0" Name="textBlock1" Foreground="#282c42" Text="标题:" VerticalAlignment="Top" /> <ListBox Margin="0,49,0,34" Name="listboxNews" ItemTemplate="{StaticResource listBoxTemplate}" BorderThickness="0"
PreviewMouseDoubleClick
="listBox1_PreviewMouseDoubleClick"> <ListBox.Background> <ImageBrush ImageSource="/Financial;component/Images/main/个人资料背景.png" /> </ListBox.Background> <ListBox.Resources> <Style TargetType="ListBoxItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#999999"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Gray"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Gray"/> </Style.Resources> </Style> </ListBox.Resources> </ListBox>

后台是通过泛型集合IList<实体>来完成绑定

      /// <summary>
        ///  初始化数据
        /// </summary>
        private void InitializeData() 
        {
            StringBuilder sqlstr = new StringBuilder();
            string key = "";
             if(txtKey.Text.Trim()!=null)
             {
                 key = txtKey.Text.Trim();
                 sqlstr.Append(" Title like '%"+key+"%'");
             }
             pager.PageSize = 5;
             WebNewsServices.News[] newList = webNews.isSelectPaging(sqlstr.ToString(), pager.PageSize, pager.PageCurrent, out _dataTotal);
             IList<WebNewsServices.News> list = new List<WebNewsServices.News>(newList);
             listboxNews.ItemsSource = list;
             //EntityListener.PfmMemberListener.Instance.Receive(list);
              }
     private void listBox1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            WebNewsServices.News newItem = listboxNews.SelectedItem as WebNewsServices.News;
            if(newItem!=null)
            {
                string id = newItem.ID;
                PassData.Id = id;
                this.ModuleExtend(true, Library.Transit.NameItems.NewDetail);
            }
         
        }