在wpf中逐个(最终)添加项目 [英] Add items one by one(eventually) in wpf

查看:63
本文介绍了在wpf中逐个(最终)添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件。后台工作者读取文本文件并将每行添加到List(Of String)。现在,在Worker的Completed事件中,我根据LIST.Now填充带有文本块的堆栈面板,因为文本文件很大(它有很多行/单词) ),doWork
事件需要很多。对我来说有任何办法,只要后台工作人员开始向LIST添加文本,堆栈面板将逐个填充文本块。例如,尽快当DoWork事件向LIST添加1个字符串时,一个文本块将添加
添加到stackpanel,当DoWork 添加第二个字符串,另一个文本块将添加到堆栈面板而不冻结UI,文本块将被添加一个一个..有没有办法做到这一点?

解决方案


>>现在,在工作室的已完成事件中,我根据列表填充带有文本块的堆栈面板。


要显示集合,您应该使用listbox,itemcontrol ...


>>。有没有办法让我这样做,一旦后台工作人员开始向LIST添加文本,堆栈面板将逐个填充文本块。


使用ObservableCollection可以使用自动动作通知添加,删除或更新哪些项目。请注意,您需要实现INotifyPropertyChanged接口。


以下是一个示例:

公共部分类TestWindow:Window,INotifyPropertyChanged 
{
public TestWindow()
{
InitializeComponent();

this.DataContext = this;

}

公共事件PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(字符串名称)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
private ObservableCollection< string>线;
public ObservableCollection< string>行
{
get
{
if(lines == null)
{
lines = new ObservableCollection< string>();
}
返回行;
}
set
{
Lines = value;
OnPropertyChanged(" Lines");
}
}


private void Button_Click(对象发送者,RoutedEventArgs e)
{
BackgroundWorker backgroundWorker = new BackgroundWorker();

backgroundWorker.DoWork + =(s,arg)=>
{


StreamReader file = new StreamReader(@"C:\ Users \V-WEDI \Desktop\txtfile.txt");

string line = null;

while((line = file.ReadLine())!= null)
{
this.Dispatcher.Invoke(()=> {Lines.Add(line) ;});

System.Threading.Thread.Sleep(1000); //模拟
}
};

backgroundWorker.RunWorkerCompleted + =(s,arg)=>
{

MessageBox.Show(" finished");
};

backgroundWorker.RunWorkerAsync();
}
}


< StackPanel> 
< Button Margin =" 10"点击= QUOT; Button_Click" Content ="加载到列表" />
< ItemsControl x:Name =" ic" ItemsSource =" {Binding Lines}"余量= QUOT; 10"高度= QUOT; 200">< / ItemsControl的>
< / StackPanel>







请不要忘记阅读您的主题并将帖子标记为解决问题的答案。


最好的问候,


Bob


I have a text file. A backgroundworker read the textfile and adds each line to a List(Of String).Now, at Worker's Completed event,i populate a stackpanel with textblocks depending on the LIST.Now,as the text file is huge(it has many lines/words),the doWork event takes a lot.Is there any way for me to , as soon as the background worker starts adding texts to the LIST,the stackpanel will be populated with textblocks one by one .For example, as soon as the DoWork event adds 1 string to the LIST,a textblock would be added be added to the stackpanel,when the DoWork  adds 2nd string, another textblock would be added to the stackpanel without freezing the UI,textblocks would be added ONE BY ONE..Is there any way to do that ?

解决方案

Hi,

>>Now, at Worker's Completed event,i populate a stackpanel with textblocks depending on the LIST.

To display a collection, you should use listbox, itemcontrol...

>>.Is there any way for me to , as soon as the background worker starts adding texts to the LIST,the stackpanel will be populated with textblocks one by one .

Use ObservableCollection which items can be added, removed or be updated with an automatic notification of actions. note that you need implement INotifyPropertyChanged interface.

Here is a sample:

 public partial class TestWindow : Window,INotifyPropertyChanged
    {
        public TestWindow()
        {
            InitializeComponent();

            this.DataContext = this;
        
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        private ObservableCollection<string> lines;
        public ObservableCollection<string> Lines
        {
            get
            {
                if (lines == null)
                {
                    lines = new ObservableCollection<string>();
                }
                return lines;
            }
            set
            {
                Lines = value;
                OnPropertyChanged("Lines");
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork += (s, arg) =>
            {
             

                StreamReader file = new StreamReader(@"C:\Users\V-WEDI\Desktop\txtfile.txt");

                string line = null;

                while (( line = file.ReadLine()) != null)
                {
                  this.Dispatcher.Invoke(()=>{ Lines.Add(line); }) ;

                    System.Threading.Thread.Sleep(1000);//simulation
                }
            };

            backgroundWorker.RunWorkerCompleted += (s, arg) =>
            {
              
                MessageBox.Show("finished");
            };

            backgroundWorker.RunWorkerAsync();
        }
    }

    <StackPanel>
        <Button Margin="10" Click="Button_Click" Content="Load to List"/>
        <ItemsControl x:Name="ic" ItemsSource="{Binding Lines}" Margin="10" Height="200"></ItemsControl>
    </StackPanel>



Please don't forget to read your threads and mark post(s) as answer which resolve your issues.

Best Regards,

Bob


这篇关于在wpf中逐个(最终)添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆