I have a long time processing operation, and for that it has to be done in the backGround, But the problem is:
- When I create the user control, and then add it to the UI, (listView) control, WPF doesn't show the UC (user control), but the listView seems to be populated withe the same number of UC i created.
I used the backgroundWorker, then i used the Dispatcher of the listView, then the main Dispatcher, but all with the same problem
i wonder if i can use the UIThread for that, but i dont know how.
My code is:
private void btn_click(object sender, System.Windows.RoutedEventArgs e)
{
string path = fileSourcesCombo.SelectedItem.ToString();
converter = getConverter(path);
this.Dispatcher.BeginInvoke(new Action(delegate()
{
System.Data.DataTable dataTable = converter.getDataTable();
dataGrid.Dispatcher.Invoke(new Action(delegate()
{
dataGrid.ItemsSource = dataTable.DefaultView;
}
));
List<MyAttribute> attributes = converter.attributes;
foreach (MyAttribute attribute in attributes)
{
string name = attribute.name;
string type = attribute.type;
CustomAttribute customAtt = new CustomAttribute(name, type);
ListViewControl.Dispatcher. Invoke(new Action(delegate() { ListViewControl.Items.Add(customAtt); }));
}
}
),System.Windows.Threading.DispatcherPriority.Background);
}
- Converter.getDataTable() takes long time.
- dataGrid.Dispatcer.Invoke works properly, because it updates and exciting control.
- ListViewControl.Dispatcher doesn't seem to work properly, neither this.Dispatcher
as i said before there is no compiling error generated, it's just that the list view seems to be populated with empty items on all of the method i tried.
EDIT :
when i changed the ListView into a ListItem it worked, but i dont know why?? any how i still would like to use the listView control instead..
This is the Xaml code where it works:
<Grid Margin=开发者_如何转开发"8,0">
<ListView x:Name="testpanel" Margin="8" BorderThickness="0" DisplayMemberPath="" Style="{DynamicResource SimpleListBox}" ItemContainerStyle="{DynamicResource SimpleListBoxItem}">
</ListView>
</Grid>
if i remover the DynemicResource from : ItemContainerStyle , it doesn't work
Please post the XAML for your ListView. Are you possibly missing a DisplayMemberPath? That is how it would behave if it has Items but does not know what to display.
It's not exactly possible to tell what is to blame but here are a few things that may or may not help:
- If you do not use the
ListView.View
you should just use aListBox
. - If you add UI-Elements to the control you should not use a
DisplayMemberPath
. - Everything inside a
BeginInvoke
should already be on the UI-thread, so those additional dispatcher-calls inside should be redundant. (Read the threading todel reference) - If
converter = getConverter(path)
is being called on a Background thread that part where you create a thread is either missing from your code or weird things are happening, i.e. calling an event-handler in code. - The control in your XAML is called
testpanel
but in the code you callListViewControl.Items.Add
, i hope that's just some inconsistency in you code-posting. - The
ItemContainerStyle
could contain all sorts of things that would mess things up.
精彩评论