You want the current data in the json file to be displayed in the listview without updating the list again or losing the SelectedInex
I already tried many ways but nothing works, it only works if you update the listview completely from ItemsSource but if I do it the selectedIndex is lost
MainPage.Xaml
<Grid RequestedTheme="Light">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="818*" />
</Grid.RowDefinitions>
<TextBox
x:Name="titulo"
Grid.Row="0"
FontSize="40"
PlaceholderText="Ingresa tu titulo"
Text="{Binding SelectedItem.title, ElementName=listNotas}"
TextChanged="Titulo_TextChanged" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<ListView
x:Name="listNotas"
Width="450"
Background="DimGray"
SelectedItem="{Binding titulo.Text, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding title, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<RichEditBox
x:Name="editor"
Width="760"
HorizontalAlignment="Stretch" />
</StackPanel>
MainPage.xaml.cs
public ObservableCollection<Notes> mynotes = new ObservableCollection<Notes>();
public string editpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Notas.json" );
public MainPage()
{
this.InitializeComponent();
// Load data of Notas.json to Listview
using (StreamReader file = File.OpenText(editpath))
{
var json = file.ReadToEnd();
baseNotes mainnotes = JsonConvert.DeserializeObject<baseNotes>(json);
foreach (var item in mainnotes.notes)
{
mynotes.Add(new Notes { title = item.title });
}
listNotas.ItemsSource = null;
listNotas.ItemsSource = mynotes;
listNotas.SelectedIndex = 0;
}
}
private void Titulo_TextChanged(object sender, TextChangedEventArgs e)
{
// Saving textbox text to title value in json file
string json = File.ReadAllText(editpath);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
int indice = listNotas.SelectedIndex;
jsonObj["notes"][indice]["title"] = titulo.Text;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);
File.WriteAllText(editpath, output);
// Show json file text in RicheditBox
editor.TextDocument.SetText(Windows.UI.Text.TextSetOptions.None, output);
}
Model class: Notes.cs
public class Notes
{
public int created { get; set; }
public string title { get; set; }
public string text { get; set; }
public int id { get; set; }
public int updated { get; set; }
}
public class baseNotes
{
public List<Notes> notes { get; set; }
}
json sample: Notas.json
{
"notes": [
{
"created": 4352346,
"title": "but not refresh listview values",
"text": "fdsgfgsd fsgf sgtryt",
"id": 432542,
"updated": 23524
},
{
"created": 4352346,
"title": "this new value",
"text": "fdsgfgsd fsgf sgtryt",
"id": 432542,
"updated": 23524
},
{
"created": 4352346,
"title": "changing value",
"text": "fdsgfgsd fsgf sgtryt",
"id": 432542,
"updated": 23524
}
]
}
Please do not know what else to do, I have been with this for 2 days, any kind of help, however minimal, will be great
Aqui una imagen:
https://i.stack.imgur.com/I4k4E.gif
as you see when writing in the textbox the changes if they are saved in the json file but they are not shown in the listview if you update it yes, but I want the changes to be displayed without updating