I'm using this RSS feed for my Windows 8 Application (c#) http://www.skysports.com/rss/0,20514,11661,00. I can display the Title and PubDate but I'm stuck on the image.
private async void LoadRSS()
{
SyndicationClient client = new SyndicationClient();
Uri feedUri = new Uri("http://www.skysports.com/rss/0,20514,11661,00.xml");
SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
FeedData feedData = new FeedData();
foreach (SyndicationItem item in feed.Items)
{
FeedItem feedItem = new FeedItem();
feedItem.Title = item.Title.Text;
feedItem.PubDate = item.PublishedDate.DateTime;
// Handle the differences between RSS and Atom feeds.
if (feed.SourceFormat == SyndicationFormat.Atom10)
{
feedItem.Content = item.Content.Text;
feedItem.Link = new Uri("http://www.skysports.com" + item.Id);
}
else if (feed.SourceFormat == SyndicationFormat.Rss20)
{
feedItem.Content = item.Summary.Text;
feedItem.Link = item.Links[0].Uri;
}
feedData.Items.Add(feedItem);
}
ItemListView.DataContext = feedData.Items;
}
xaml code:
<GridView x:Name="ItemListView" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding}" ItemClick="Sports_ItemClick_1" IsItemClickEnabled="True" SelectionMode="None" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid Width="400" Height="75" Margin="0,0,50,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition Width="325"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="#60BF89">
<Image Source="{Binding ImagePath, Mode=OneWay}" Stretch="None" Margin="5,15,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Width="500"/>
</Border>
<StackPanel Grid.Column="1" VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Foreground="White" Text="{Binding Title}" FontSize="16" Margin="5,0,0,0" TextWrapping="Wrap" />
<TextBlock Foreground="White" Text="{Binding PubDate}" FontSize="12" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
I would like the Image in the first column. Any ideas??
you are not setting ImagePath property in the code sample you showed, considering this as a problem
Related
I have an ItemsControl with DataTemplate in my Page.Xaml and the code is like below:
<ItemsControl x:Name="chatUI" VerticalAlignment="Bottom">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="myGrid" Width="340" Background="{Binding Background}" HorizontalAlignment="{Binding GridHorizontalAlign}" Margin="10,0,10,10" MinHeight="45" BorderBrush="#FF003A4F" BorderThickness="0,0,0,2">
<Polygon Visibility="{Binding RightVisibility}" Fill="{Binding Background}" Points="0,0 5,6, 0,12" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,-5,0" />
<Polygon Visibility="{Binding LeftVisibility}" Fill="{Binding Background}" Points="5,0 0,6, 5,12" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-5,0,0,0" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="15" FontFamily="Segoe UI" Foreground="White" Margin="10,10,10,0"/>
<TextBlock Grid.Row="1" Text="{Binding Time}" TextWrapping="Wrap" FontSize="11" FontFamily="Segoe UI" Foreground="LightGray" Margin="10,0,10,5" VerticalAlignment="Bottom" TextAlignment="Right"/>
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
What I need right now is getting Text which is bound to the TextBlock when I right click on the grid named myGrid. How is that possible in C#?
We can add the RightTapped event of the Grid, it will be fired when you right click the Grid.
In the RightTapped event we can use Grid.Children to get the collection of child elements of the Grid. That we can get the Grid in the root Grid that named myGrid. That we can use the Grid.Children to get the TextBlock in the Grid.
For example:
private async void myGrid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
var RightTapGrid = sender as Grid;
var childernElements = RightTapGrid.Children;
foreach (var item in childernElements)
{
var grid = item as Grid;
if (grid != null)
{
var itemchildernElements = grid.Children;
foreach (var text in itemchildernElements)
{
var textBlock = text as TextBlock;
var dialog = new ContentDialog()
{
Title = textBlock.Text,
MaxWidth = this.ActualWidth
};
dialog.PrimaryButtonText = "OK";
dialog.SecondaryButtonText = "Cancel";
await dialog.ShowAsync();
break;
}
}
}
}
If you get your Binding Data from Class called ClassName
You can try this code
XAML:
<ListView x:Name="chatUI" VerticalAlignment="Bottom" SelectionChanged="chatUI_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="myGrid" Width="340" Background="{Binding Background}" HorizontalAlignment="{Binding GridHorizontalAlign}" Margin="10,0,10,10" MinHeight="45" BorderBrush="#FF003A4F" BorderThickness="0,0,0,2">
<Polygon Visibility="{Binding RightVisibility}" Fill="{Binding Background}" Points="0,0 5,6, 0,12" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,-5,0" />
<Polygon Visibility="{Binding LeftVisibility}" Fill="{Binding Background}" Points="5,0 0,6, 5,12" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-5,0,0,0" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="15" FontFamily="Segoe UI" Foreground="White" Margin="10,10,10,0"/>
<TextBlock Grid.Row="1" Text="{Binding Time}" TextWrapping="Wrap" FontSize="11" FontFamily="Segoe UI" Foreground="LightGray" Margin="10,0,10,5" VerticalAlignment="Bottom" TextAlignment="Right"/>
</Grid>
</Grid>
</DataTemplate>
</Listview.ItemTemplate>
And add SelectionChanged Event :
private void chatUI_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView view = (ListView)sender;
//Get Selected Item
ClassName class = view.SelectedItem as ClassName;
string path = class.Text;
// Now we have Text of selected item in Listview
}
I have a gridview with data bindings that contain titles and book covers. Cover the book is in the local package with the name of the path: "files/kurikulum 2006/[The name of the folder selected by the user on the previous page]/cover".I have trouble displaying the cover of the book so that the book cover does not appear and the error message appears as below:
But for the title can be displayed on the gridview.
XAML:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Margin="0,0,10,0"
HorizontalAlignment="Center"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick" BorderThickness="0">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="315" Width="200" Margin="5,10,0,0" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Margin="10,10,10,10" Height="230" Width="180" Source="{Binding Image}" AutomationProperties.Name="{Binding Name}" />
<Grid Grid.Row="1" Margin="0,0,10,10" HorizontalAlignment="Left" Height="50" >
<ScrollViewer Margin="10,10,5,5" VerticalAlignment="Top" HorizontalAlignment="Left" Height="40" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<TextBlock Margin="0,0,10,10" Text="{Binding Name}" Foreground="Black" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI Black" FontWeight="SemiBold" TextAlignment="Center" TextWrapping="Wrap" Height="40" Width="auto"/>
</ScrollViewer>
</Grid>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Code:
ObservableCollection<Book> datasource = new ObservableCollection<Book>();
StorageFolder _folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
_folder = await _folder.GetFolderAsync("files");
_folder = await _folder.GetFolderAsync(direktori.Direktori);
_folder = await _folder.GetFolderAsync(direktori.Name);
_folder = await _folder.GetFolderAsync("cover");
IReadOnlyList<StorageFile> _files = await _folder.GetFilesAsync(); //which returns List<StorageFile>
foreach (StorageFile file in _files)
{
Book buku = new Book();
buku.Name = file.DisplayName.ToString();
StorageFile thumbFile;
thumbFile = await _folder.GetFileAsync(file.DisplayName.ToString() + ".jpg");
BitmapImage bi = new BitmapImage();
bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
buku.Image = bi;
datasource.Add(buku);
}
itemGridView.ItemsSource = datasource;
Book class:
public class Book
{
public string Name { get; set; }
public string Direktori { get; set; }
public ImageSource Image { get; set; }
}
How to handle it?
Note:
direktori.Direktori = kurikulum 2006 (or the other folder)
direktori.Name = The name of the folder selected by the user on the previous page
I have json as below:
I want to display the data in the gallery on the gridview, then if the user clicks the image on the gridview, it will show a picture with a larger size on the flipview according to the selected image. I'm having trouble, that is when the image on the gridview is selected, it always shows up the image on the flipview for the first index (not in accordance with the picture chosen by the user).
XAML:
<callisto:CustomDialog x:FieldModifier="public" x:Name="ArticleDetail"
Grid.Row="0" Grid.RowSpan="2" Title=""
Background="White" BackButtonVisibility="Collapsed" BorderBrush="White" >
<ScrollViewer Margin="0,0,5,5" Background="{x:Null}" VerticalScrollBarVisibility="Auto" HorizontalScrollMode="Disabled">
<StackPanel Height="auto">
<ProgressRing x:Name="articleDetailLoading" HorizontalAlignment="Center" VerticalAlignment="Center" Height="50" Width="50" />
<StackPanel x:Name="articleDetailBox" Margin="0,0,10,10" Height="auto">
<Grid x:Name="articleDetailGrid" BorderBrush="#FF990000" BorderThickness="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="articleIdDetail" Grid.Row="0" Text="{Binding ID}" Visibility="Collapsed"/>
<AppBarButton x:Name="closeArticleBtn" Icon="Clear" HorizontalAlignment="Right" Foreground="Black" Click="closeArticleBtn_Click"/>
<ScrollViewer x:Name="articleTitleScroll" Grid.Row="1" Margin="10,0,0,0" Width="650" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" VerticalScrollBarVisibility="Auto" Background="{x:Null}" HorizontalScrollMode="Disabled">
<TextBlock x:Name="articleTitleDetail" Margin="0,0,15,15" FontSize="18" FontFamily="Segoe UI Black" FontWeight="SemiBold" TextWrapping="Wrap" Height="auto" TextAlignment="Center"/>
</ScrollViewer>
<TextBlock x:Name="articleDateDetail" Grid.Row="3" Margin="10,15,10,0" FontSize="15" FontFamily="Segoe UI Black" FontWeight="SemiLight" TextWrapping="Wrap" Height="auto" TextAlignment="Center"/>
<GridView x:Name="articleImageGridView" Grid.Row="5" Margin="10,25,10,0" Height="110" AutomationProperties.AutomationId="ItemDetailScrollViewer" DataContext="{Binding SelectedItem, ElementName=itemListView}"
ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto" IsItemClickEnabled="True"
ScrollViewer.ZoomMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemClick="articleImageGridView_ItemClick">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="90" Width="120" Margin="5,0,0,0" Background="{x:Null}" BorderBrush="#FF646464" BorderThickness="0.5">
<Border x:Name="coverBox" Grid.Row="0" Margin="0,0,0,0" Width="120" Height="90" VerticalAlignment="Top" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="0.5">
<Border.Background>
<ImageBrush Stretch="UniformToFill" ImageSource="images/IP-placeholder.png"/>
</Border.Background>
<Image x:Name="cover" Source="{Binding ImageURL1}" HorizontalAlignment="Center" Stretch="Uniform" AutomationProperties.Name="{Binding Title}" ImageOpened="Image_ImageOpened" Loaded="cover_Loaded" Loading="cover_Loading"/>
</Border>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
<WebView x:Name="articleContentDetail" Grid.Row="6" Margin="10,15,10,10" Width="650" Height="150" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</StackPanel>
<StackPanel x:Name="articleFullImageDetailBox" Margin="0,0,10,10" Height="auto" Visibility="Collapsed">
<Grid x:Name="articleFullImageDetailGrid" BorderBrush="#FF990000" BorderThickness="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<AppBarButton x:Name="closearticleFullImageBtn" Grid.Column="1" Icon="Clear" HorizontalAlignment="Right" Foreground="Black" Click="closearticleFullImageBtn_Click"/>
<TextBlock x:Name="articleFullIdDetail" Grid.Row="0" Text="{Binding ID}" Visibility="Collapsed"/>
<FlipView x:Name="articleImageFullFlipview" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Margin="0,0,0,0" ItemsSource="{Binding gallery.Items}" SelectedItem="{Binding gallery, Mode=TwoWay}" Width="650" Height="500" VerticalAlignment="Center" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Background="{x:Null}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid x:Name="content" Margin="0,0,0,0">
<Border x:Name="coverBox" Width="600" Height="500">
<Border.Background>
<ImageBrush Stretch="Uniform" ImageSource="images/IP-placeholder.png"/>
</Border.Background>
<Image x:Name="cover" Source="{Binding ImageURLFull}" HorizontalAlignment="Center" Stretch="UniformToFill" AutomationProperties.Name="{Binding ID}" ImageOpened="Image_ImageOpened" Loaded="cover_Loaded" Loading="cover_Loading"/>
</Border>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
</StackPanel>
</StackPanel>
</ScrollViewer>
</callisto:CustomDialog>
Code:
ObservableCollection<ArticleClass> articleDatasourceDetailImage = new ObservableCollection<ArticleClass>();
articleDatasourceDetailImage.Clear();
articleDetailLoading.Visibility = Visibility.Visible;
articleDetailLoading.IsActive = true;
articleDetailBox.Visibility = Visibility.Collapsed;
articleFullImageDetailBox.Visibility = Visibility.Collapsed;
ArticleDetail.IsOpen = true;
articleItemDetail = e.ClickedItem as ArticleClass;
articleIdDetail.Text = articleItemDetail.ID.ToString();
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
{
articleDetailLoading.IsActive = true;
try
{
string urlPath = "http://indonesia-product.com/api/v1/articles/"10.json?module=articles&page=1&token=3f63-dc43-c8d5-eb45-8cbf-b72d-9d98-800f";
//Debug.WriteLine(urlPath.ToString());
var httpClient = new HttpClient(new HttpClientHandler());
var values = new List<KeyValuePair<string, string>>
{
};
HttpResponseMessage response = await httpClient.GetAsync(urlPath);
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
articleDetailLoading.IsActive = false;
RequestException();
}
string jsonText = await response.Content.ReadAsStringAsync();
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonObject jsonData = jsonObject["data"].GetObject();
JsonObject groupObject1 = jsonData.GetObject();
double id = groupObject1["id"].GetNumber();
string title = groupObject1["title"].GetString();
string date = groupObject1["publication_date"].GetString();
JsonArray jsonGallery = groupObject1["gallery"].GetArray();
foreach (JsonValue groupValue1 in jsonGallery)
{
JsonObject groupObject2 = groupValue1.GetObject();
string imageUrl = groupObject2.ContainsKey("image_url") && groupObject2["image_url"] != null ? groupObject2["image_url"].GetString() : string.Empty;
ArticleClass fileImage = new ArticleClass();
fileImage.ImageURL1 = imageUrl;
articleDatasourceDetailImage.Add(fileImage);
}
articleImageGridView.ItemsSource = articleDatasourceDetailImage;
articleDetailLoading.IsActive = false;
ArticleClass file = new ArticleClass();
file.ID = Convert.ToInt32(id);
file.Title = title;
DateTime dateConvert = Convert.ToDateTime(date);
file.Date = dateConvert.ToString("MMM d, yyyy");
articleContentDetail.NavigateToString(contentText);
articleDetailLoading.IsActive = false;
articleDetailBox.Visibility = Visibility.Visible;
articleDetailLoading.Visibility = Visibility.Collapsed;
articleDetailLoading.IsActive = false;
}
catch (HttpRequestException ex)
{
articleDetailLoading.IsActive = false;
}
}
else
{
articleDetailLoading.IsActive = false;
}
}
private async void articleImageGridView_ItemClick(object sender, ItemClickEventArgs e)
{
articleDetailLoading.Visibility = Visibility.Visible;
articleDetailLoading.IsActive = true;
articleDetailBox.Visibility = Visibility.Collapsed;
articleFullImageDetailBox.Visibility = Visibility.Collapsed;
var articleImageGridView = (GridView)sender;
articleImageFullFlipview.SelectedIndex = articleImageGridView.SelectedIndex;
articleFullImageDetailBox.Visibility = Visibility.Visible;
articleDetailLoading.Visibility = Visibility.Collapsed;
articleDetailLoading.IsActive = false;
}
How to handle it?
First, there is no need to get your data again when clicking on the item. When you load your data, set the ItemsSource of your GridView and your FlipView to be the same collection.
Second, when your item is clicked, get the SelectedIndex of the item and then set the SelectedIndex of your FlipView
private async Task LoadData()
{
// get data
var response = await httpClient.GetAsync(url);
// Convert to your objects
articleImageFullFlipview.ItemsSource = data;
articleImageGridView.ItemsSource = data;
}
private void articleImageGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var gridView = (GridView)sender;
articleImageFullFlipview.SelectedIndex = gridView.SelectedIndex;
// show or hide any controls
}
I am new to c# WPF app developing, currently I write my first application and I was able to display a list on a window but only in one box.
Fitness box works perfectly but I can't get second one to work. If I change the order and put WeightList first then it works and ActivityList is not displayed. Here is the code:
XAML:
<Grid>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="0,3,0,0">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="FITNESS"/></TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="88*"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<ListView HorizontalAlignment="Left"
Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
x:Name="ActivityList" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Type"
DisplayMemberBinding="{Binding Type}"/>
<GridViewColumn Header="Date"
DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Distance"
DisplayMemberBinding="{Binding Distance}"/>
</GridView>
</ListView.View>
</ListView>
<Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Click="Button_Click" Opacity="0.8">
<Grid HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
</Grid>
</Button>
</Grid>
</Grid>
</Border>
<Expander Header="NOTES" HorizontalAlignment="Left" Margin="0,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
<StackPanel Margin="10,4,0,0">
<TextBox Margin="4" />
</StackPanel>
</Expander>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="238,3,0,0">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="WEIGHT CONTROL"/></TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="88*"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<ListView HorizontalAlignment="Left"
Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
x:Name="WeightList" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Date"
DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Weight"
DisplayMemberBinding="{Binding currentWeight}"/>
</GridView>
</ListView.View>
</ListView>
<Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Opacity="0.8">
<Grid HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
</Grid>
</Button>
</Grid>
</Grid>
</Border>
<Expander Header="NOTES" HorizontalAlignment="Left" Margin="238,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
<StackPanel Margin="10,4,0,0">
<TextBox Margin="4" />
</StackPanel>
</Expander>
</Grid>
MainWindow.cs
public partial class MainWindow : ModernWindow
{
public static ObservableCollection<Activity> Activities;
public static ObservableCollection<Weight> WeightControl;
public MainWindow()
{
InitializeComponent();
Activities = new ObservableCollection<Activity>() {
new Activity() {Type = "Running", Date = "15.07.2015", Distance = "5km"},
new Activity() {Type = "Cycling", Date = "17.07.2015", Distance = "120km"},
new Activity() {Type = "Swimming", Date = "19.07.2015", Distance = "3km"},
};
ActivityList.ItemsSource = Activities;
WeightControl = new ObservableCollection<Weight>() {
new Weight() {Date = "15.07.2015", currentWeight = 61.2},
new Weight() {Date = "17.07.2015", currentWeight = 62.1},
new Weight() {Date = "19.07.2015", currentWeight = 61.9},
};
Image with problem
You forgot to set WeightList.ItemsSource. this should do it:
WeightList.ItemsSource = WeightControl;
i want delete selected item from my listbox using context menu drop-down box here is my xaml
<ListBox Margin="3,60,1,10" Grid.Row="1" Name="lstNews" Tap="lstNews_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Row="1" Orientation="Horizontal" Height="120" Width="478">
<StackPanel.Background>
<ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
</StackPanel.Background>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<toolkit:MenuItem Header="Open" Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid HorizontalAlignment="Left" Width="30" Background="#FF0195D5" Margin="0,0,0,2" Height="118">
<TextBlock x:Name="txtDate" TextWrapping="Wrap" Text="{Binding Path=newsDate}" RenderTransformOrigin="0.5,0.5" Margin="-43.169,44.001,-43.831,0" UseLayoutRounding="False" d:LayoutRounding="Auto" TextAlignment="Center" Height="30" VerticalAlignment="Top" Width="117">
<TextBlock.RenderTransform>
<CompositeTransform Rotation="-90"/>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
<Grid HorizontalAlignment="Left" Width="5" Height="120"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">
<TextBlock x:Name="txtTitle" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsTitle}" Foreground="Black" FontSize="18.667" HorizontalAlignment="Left" Width="432" FontWeight="Bold" />
<StackPanel Orientation="Horizontal" Width="432" Height="27">
<TextBlock x:Name="txtBy" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsSource}" Foreground="Black" FontSize="18.667" Width="399"/>
<Image x:Name="imgArrow" Width="25" Source="Images/Go-In-Arrow.png" Height="25" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Width="433" Height="60">
<TextBlock x:Name="txtDesc" Height="58" TextWrapping="Wrap" Foreground="Black" Text="{Binding Path=newsShortDescription}" FontSize="18.667" Width="371"/>
<TextBlock x:Name="txtID" Height="56" Text="{Binding Path=newsID}" TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>
<Image x:Name="imgType" Width="35" Source="{Binding Path=newsTypeImage}" Height="40" Margin="27,20,0,0" d:LayoutOverrides="Height"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
here is what i am trying to do on delete event "MenuItem_Click" but than it throws error "Operation not supported on read-only collection.so what is code to delete selected item on that click event
// this is code to delete i am trying--> lstNews.Items.Remove(lstNews.SelectedItem.ToString());
//below is code im trying to set listboxitemsource
private void FillListBox()
{
fulllist = new nList();
lstNews.ItemsSource = fulllist;
}
public class nList : List<NewsData>
{
StringData sd = new StringData();
public IList<NewsData> GetLCList()
{
IList<NewsData> lcList = null;
using (NewsDataContext context = new NewsDataContext(sd.news_string))
{
IQueryable<NewsData> query = (from app in context.NewsData select app).OrderByDescending(app => app.entryID);
lcList = query.ToList();
}
return lcList;
}
public nList()
{
IList<NewsData> lcData = this.GetLCList();
StringBuilder messageBuilder = new StringBuilder();
foreach (NewsData lc in lcData)
{
Add(new NewsData
{
newsID = lc.newsID,
newsTitle = lc.newsTitle,
newsSource = lc.newsSource,
newsDate = (new GetDate()).getdate(lc.newsDate),//(new AnnouncementList()).getdate(lc.newsDate),
newsShortDescription = lc.newsShortDescription,
newsTypeImage = lc.newsTypeImage,
newsSharing = lc.newsSharing
});
}
}
}
lstNews.Items is list of object that are displayed on the page. So this lstNews.Items is collection of your datatemplate so that is why when you tried lstNews.Items.Remove(lstNews.SelectedItem.ToString()) than this fails.
you should use lstNews.Items.Remove(lstNews.SelectedItem) to delete item.
But for best practice it is prefered to delete item from the source not from the list. i.e. You should delete item from fulllist and reassign it as lstNews.ItemsSource = fulllist;
Changes done in your code
fulllist should be a type of ObservableCollection so that all the changes done on data can be reflected to UI.
To convert List to ObservableCollection Following code can be used:
fulllist = new ObservableCollection<NewsData>(new nList());
Add the implementation for deleting data from fulllist a possible implementation could be:
object obj = lstNews.SelectedItem;
if(obj is NewsData){
fulllist.Remove((NewsData)obj);
lstNews.ItemsSource = fulllist;
}