json文件修改时如何实时更改listview值
How to change listview values in real time when the json file is modified
您希望json文件中的当前数据显示在列表视图中,而不需要再次更新列表或丢失SelectedInex
我已经尝试了很多方法,但没有任何效果,只有当您从 ItemsSource 完全更新列表视图时它才有效,但如果我这样做,selectedIndex 会丢失
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);
}
型号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 样本: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
}
]
}
请不知道还能做什么,我已经用了 2 天了,任何形式的帮助,无论多么微不足道,都会很棒
Aqui una imagen:
https://i.imgur.com/3619gg3.gif
正如您在文本框中看到的那样,如果更改保存在 json 文件中,但如果您更新它,它们不会显示在列表视图中,是的,但我希望更改不显示更新
首先,您需要实现 INotifyPropertyChanged 并订阅 PropertyChanged event.In 这种情况下,标题 属性 是 observable.When 您的标题已更改或 UI 文本已更改,它将收到更新通知。
public class Notes: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public int created { get; set; }
private string myTitle;
public string title {
get {
return myTitle;
}
set {
myTitle = value;
OnPropertyChanged();
}
}
public string text { get; set; }
public int id { get; set; }
public int updated { get; set; }
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
那么,TextBox 的绑定模式应该是 TwoWay 并且 UpdateSourceTrigger 应该是 PropertyChanged。这意味着如果你想要源在您键入时更新,您需要将绑定的 UpdateSourceTrigger 设置为 PropertyChanged.In 这种情况下,当您在 TextBox 中键入时,它会通知它绑定的标题,并且 listView 中的 textBlock 也会更新。
.xaml:
<TextBox x:Name="titulo"
Grid.Row="0"
FontSize="40"
PlaceholderText="Ingresa tu titulo"
Text="{Binding SelectedItem.title, ElementName=listNotas,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
TextChanged="Titulo_TextChanged" />
您希望json文件中的当前数据显示在列表视图中,而不需要再次更新列表或丢失SelectedInex
我已经尝试了很多方法,但没有任何效果,只有当您从 ItemsSource 完全更新列表视图时它才有效,但如果我这样做,selectedIndex 会丢失
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);
}
型号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 样本: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
}
]
}
请不知道还能做什么,我已经用了 2 天了,任何形式的帮助,无论多么微不足道,都会很棒
Aqui una imagen:
https://i.imgur.com/3619gg3.gif
正如您在文本框中看到的那样,如果更改保存在 json 文件中,但如果您更新它,它们不会显示在列表视图中,是的,但我希望更改不显示更新
首先,您需要实现 INotifyPropertyChanged 并订阅 PropertyChanged event.In 这种情况下,标题 属性 是 observable.When 您的标题已更改或 UI 文本已更改,它将收到更新通知。
public class Notes: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public int created { get; set; }
private string myTitle;
public string title {
get {
return myTitle;
}
set {
myTitle = value;
OnPropertyChanged();
}
}
public string text { get; set; }
public int id { get; set; }
public int updated { get; set; }
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
那么,TextBox 的绑定模式应该是 TwoWay 并且 UpdateSourceTrigger 应该是 PropertyChanged。这意味着如果你想要源在您键入时更新,您需要将绑定的 UpdateSourceTrigger 设置为 PropertyChanged.In 这种情况下,当您在 TextBox 中键入时,它会通知它绑定的标题,并且 listView 中的 textBlock 也会更新。
.xaml:
<TextBox x:Name="titulo"
Grid.Row="0"
FontSize="40"
PlaceholderText="Ingresa tu titulo"
Text="{Binding SelectedItem.title, ElementName=listNotas,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
TextChanged="Titulo_TextChanged" />