如何在Telerik RadDataBoundListBox中重置或删除旧数据 Json解析并显示在RadDataBoundListBox
中RadDataBoundListBox每次加载旧数据,如果我点击按钮加载我要删除的新数据或重置旧数据,因为每次显示旧数据
public void getMainData()
{
string mainUrlData = "http://www.mydomain.com/app.json";
WebClient wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri(mainUrlData));
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
string result = e.Result.ToString();
JsonConvert.PopulateObject(result, PopulateData);
NewsList.ItemsSource = PopulateData;
}
}
答案 0 :(得分:1)
迟到了,希望这会有所帮助..
使用MVVMLight
使用HttpClient:
public async Task<ObservableCollection<T>> GetAll()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(jsonMediaType));
//if (useToken)
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var json = await client.GetStringAsync(String.Format("{0}{1}{2}", apiUrl, addressSuffix, apiKey)).ConfigureAwait(false);
Debug.WriteLine(json);
JObject o = JObject.Parse(json);
Debug.WriteLine(o);
return await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<ObservableCollection<T>>(o[moduleName].ToString()));
}
的ItemSource:
private ObservableCollection<Ticket> _myTickets;
public ObservableCollection<Ticket> MyTickets
{
get
{
return _myTickets;
}
set
{
_myTickets = value;
RaisePropertyChanged(() => MyTickets);
}
}
Telerik RadBoundListBox:
<telerikPrimitives:RadDataBoundListBox
x:Name="radDataBoundListBox"
ItemsSource="{Binding TicketList}"
ItemTemplate="{StaticResource ListBoxItemTemplate}"
ItemContainerStyle="{StaticResource ItemContainerStyle}"
SelectedItem="{Binding SelectedTicket, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="750"
ScrollViewer.VerticalScrollBarVisibility="Auto"
UseOptimizedManipulationRouting="False"
EmptyContent="Laden..."
IsPullToRefreshEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="RefreshRequested">
<cmd:EventToCommand
Command="{Binding RefreshRequested}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</telerikPrimitives:RadDataBoundListBox>
刷新命令:
private RelayCommand _refreshCommand;
public RelayCommand RefreshRequested
{
get
{
return _refreshCommand
?? (_refreshCommand = new RelayCommand(
() =>
{
ExecuteRefreshCommand();
}));
}
}
public async void ExecuteRefreshCommand()
{
var tickets = await _dataService.GetAll();
if (tickets != null)
{
_ticketList.Clear();
foreach (var ticket in tickets)
{
_ticketList.Add(ticket);
Debug.WriteLine(ticket.name);
}
Messenger.Default.Send(new HandleViewMessage() { StopPullToRefresh = true });
RaisePropertyChanged(() => TicketList);
}
}