Reputation: 1826
I have a function which updates every 5 seconds. I would like to keep the selected item but the item does not get selected.
The function below updates every 5 seconds and updates the list in the list view:
C#
public void festJSONUpdateEventHandler()
{
var tempfest = Workspace.This.festStats.Selectedfest;
//REFRESH BINDINGS HERE!!
Workspace.This.festStats.festItems = MainWindow._fest.festData.fest_Items;
Workspace.This.festStats.Selectedfest = tempfest;
}
XAML
<DataTemplate>
<StackPanel Orientation="Vertical">
<ListView x:Name="lvfest" ItemsSource="{Binding festItems}" SelectedItem="{Binding Selectedfest, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" >
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding id}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="ID" Click="lvfestColumnHeader_Click">ID</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding formatType}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Format" Click="lvfestColumnHeader_Click">Format</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding modifiedIso8601}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Date" Click="lvfestColumnHeader_Click">Date</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
More C#
class festViewModel : ToolViewModel
{
public festItem _selectedfest;
public festItem Selectedfest
{
get { return _selectedfest; }
set
{
if (_selectedfest != value)
{
_selectedfest = value;
RaisePropertyChanged("Selectedfest");
}
}
}
private List<festItem> _festItems;
public List<festItem> festItems
{
get { return _festItems; }
set
{
if (_festItems != value)
{
_festItems = value;
RaisePropertyChanged("festItems");
}
}
}
}
Upvotes: 0
Views: 842
Reputation: 2821
From comments:
I have a library that extracts JSON data and in there, there is a list of objects called fest_Items. from this list, I send the objects to festItems list in the festViewModel which is bound to my list view.
Is the list being recreated at each function call (ergo 5 seconds)? It may be because your items do not contains the same reference to the object.
Two or more objects sharing the same values may be not the same references. If you are recreating the list, that may be the cause of the binding failing to retreive the item.
As a solution, I suggest finding an unique property (like the ID property of your model) and search the item in the newly created list.
Exemple:
public void festJSONUpdateEventHandler()
{
var tempfestID = Workspace.This.festStats.Selectedfest.ID;
//REFRESH BINDINGS HERE!!
Workspace.This.festStats.festItems = MainWindow._fest.festData.fest_Items;
// Find the first festStat that has the same ID as the old selected one.
Workspace.This.festStats.Selectedfest = Workspace.This.festStats.FirstOrDefault(x => x.ID == tempfestID);
}
Let me know if it is not working.
Upvotes: 1