关闭

在ListBoxItem中增加Button实现添加继续添加ListBoxItem

1242人阅读 评论(0) 收藏 举报
分类:

近段在做一个功能,在ListBoxItem中点击Button 实现添加Item并选中最新添加项,把代码分享出来。

xaml:

<Window x:Class="WpfCustomerControl.ListBoxWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListBoxWindow" Height="300" Width="300" WindowStartupLocation="CenterScreen" >
    <Window.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
            <Setter Property="Margin" Value="20,5,5,5"></Setter>
            <Setter Property="Height" Value="50"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid x:Name="grid" Background="Gray">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBox Width="120" Text="{TemplateBinding Content}"></TextBox>
                            <Button x:Name="btnAdd" Width="60" Content="+" Grid.Column="1" PreviewMouseLeftButtonDown="btnAdd_MouseLeftButtonDown"></Button>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="grid" Property="Background" Value="Red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="lbData"></ListBox>
    </Grid>
</Window>
后台代码:
  public partial class ListBoxWindow : Window
    {
        public ListBoxWindow()
        {
            InitializeComponent();
            this.lbData.Items.Add(DateTime.Now.ToString("yyyyMMdd hh:mm:ss"));
        }
        private void btnAdd_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            object obj = e.OriginalSource;
            this.lbData.Items.Add(DateTime.Now.ToString("yyyyMMdd hh:mm:ss"));

            e.Handled = true;
            this.lbData.SelectedIndex = this.lbData.Items.Count - 1;
            ScrollViewer sv = this.GetChildObject<ScrollViewer>(lbData, "");
            if (sv != null)
                sv.ScrollToBottom();  // 滚动到末尾
        }

        public T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
        {
            DependencyObject child = null;
            T grandChild = null;
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);
                if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))
                {
                    return (T)child;
                }
                else
                {
                    grandChild = GetChildObject<T>(child, name);
                    if (grandChild != null)
                        return grandChild;
                }
            }
            return null;
        }
    }

效果:



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103137次
    • 积分:1375
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:1篇
    • 评论:23条
    文章分类
    最新评论