问题
So I am trying to move a desktop program i made to an app for Windows 8.1
using Visual Studio 2013
. Datagrid
was deprecated for this release so I am trying to translate those over to a ListView
. I previously used ListViews
in other desktop applications, but it looks like a lot has changed. The problem I am having is populating my ListView
from my database. I am connecting through a WCF Service
and stepping through the code, I can see I am getting the right data, I just cant get it to appear in my ListView. My preferred end result would be a 'listview' with 3 columns of information which will be editable. Previously I would use ListView.View
and then put a GridView
in there for the columns. But it appears ListView.View
is deprecated just like GridView.Columns
.
here is my xaml for the ListView
<ListView x:Name="lvInventory" Grid.Row="2" Style="{StaticResource listViewStyle}" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="200" Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="tbName" Text="{Binding InventoryName}" Width="200" Foreground="#FF0E0D0D" />
<TextBox x:Name="tbQty" Grid.Column="1"/>
<TextBox x:Name="tbType" Grid.Column="2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In my codebehind I am assigning the itemssource of my listview.
VMInventory inventory = new VMInventory();
inventory.GetList();
lvInventory.ItemsSource = inventory;
VMInventory
is my ViewModel
where I am getting my data from the WCF service
and that looks like:
public async Task GetList()
{
this.connection = new InventoryModelEntities(new Uri(url));
var filteredList = from o in connection.Inventory
where o.Have == false
select o;
var query = await Task.Factory.FromAsync((filteredList as DataServiceQuery).BeginExecute(null, null),
(result) => (filteredList as DataServiceQuery).EndExecute(result)) as IEnumerable<Aurora.InventoryService.Inventory>;
this.inventoryList = query.ToList();
this.currentItem = 0;
this.onPropertyChanged("Current");
this.IsAtStart = true;
this.IsAtEnd = (inventoryList.Count == 0);
}
One last side note, I was able to add a textbox to the Grid
and when I DataBind
it to text="{Binding Current.InventoryName}"
I am able to successfully bind it.
回答1:
If i understand correctly your UI is not getting updated with values what ever your adding in ViewModel, but if you add some value in UI it is reflecting in the ViewModel.
If that is the case, the use ObserableCollection instead of list, and if your creating list every time, then you have to implement INotifyPropertyChanged in your ViewModel.
If you have done all this but still it is not updating then, your creating the list in asy Task, which is not a UI thread. If you want to update a UI from a non-Ui thread, then use update the UI using Dispatcher. You can find lot of examples to update the UI from non-UI thread using dispatcher
来源:https://stackoverflow.com/questions/20005641/listview-data-binding-for-windows-8-1-store-apps