Problems with deleting items from a listbox - windows-phone-7

The behaviour Im trying to achieve is to press and hold a listbox item and I should be able to delete the item.
I have created the listbox through code. Everylistitem has a stackpanel which holds two textblocks, ie a date and a string.
How can i get press and hold event for a selectedlistitem?? I have not used binding anywhere.
C#:
for (int i = 0; i < iHistCount; i++)
{
TextBlock dateText = new TextBlock();
TextBlock durationText = new TextBlock();
TextBlock spacer = new TextBlock();
TextBlock spacer2 = new TextBlock();
dateText.FontFamily = (FontFamily)App.Current.Resources["CicleFina"];
durationText.FontFamily = (FontFamily)App.Current.Resources["CicleFina"];
dateText.FontSize = 25;
durationText.FontSize = 25;
dateText.TextAlignment = TextAlignment.Right;
durationText.TextAlignment = TextAlignment.Left;
dateText.VerticalAlignment = System.Windows.VerticalAlignment.Center;
durationText.VerticalAlignment = System.Windows.VerticalAlignment.Center;
spacer.Width = 30;
dateText.Width = 130;
spacer2.Width = 50;
durationText.Width = 170;
DateTime dtHist = pCycMan.GetHistoryDate(i);
strDisplay = dtHist.ToString("dd-MMM-yyyy");
dateText.Text = strDisplay;
if( condition)
{
// logic
durationText.Text = strDisplay;
}
StackPanel st = new StackPanel();
st.Height = 50;
st.Orientation = System.Windows.Controls.Orientation.Horizontal;
st.Children.Add(spacer);
st.Children.Add(dateText);
st.Children.Add(spacer2);
st.Children.Add(durationText);
listboxHistory.Items.Add(st);
}
XAML
<ListBox x:Name="listboxHistory" Height="280" Canvas.Left="60" Canvas.Top="232" Width="360" Foreground="Gray">

Assuming you're using the Silverlight toolkit for a context menu, this link is a good example, and has how to create the menu in code-behind.
http://windowsphonegeek.com/articles/WP7-ContextMenu-in-depth--Part1-key-concepts-and-API
For example here:
StackPanel st = new StackPanel();
st.Height = 50;
st.Orientation = System.Windows.Controls.Orientation.Horizontal;
st.Children.Add(spacer);
st.Children.Add(dateText);
st.Children.Add(spacer2);
st.Children.Add(durationText);
listboxHistory.Items.Add(st);
ContextMenu cm = new ContextMenu();
MenuItem delete = new MenuItem()
{
Header = "Delete",
Tag= "Delete" //you could also put the item itself here, would be a good reference
};
delete.Clicked += //your event handler
MenuItem edit = new MenuItem()
{
Header = "Edit",
Tag = "Edit", //you could also put the item itself here, would be a good reference
};
edit.Clicked += //your event handler
//add the items to the context menu.
cm.Items.Add(delete);
cm.Items.Add(edit);
ContextMenuService.SetContextMenu(st, cm);

Related

how to add button n times on xamarin

First of all i have seen few topics about that but didnt helped i typed thoose in xamarin.
i add a button in that way
Button desk = new Button
{
Text = desknumber.ToString(),
Font = Font.SystemFontOfSize(NamedSize.Large),
BorderWidth = 1,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
so i have to run this command in loop or in a method like that
public void masaekle(int masasayisi)
{
Button desknumber = new Button
{
Text = masasayisi.ToString(),
Font = Font.SystemFontOfSize(NamedSize.Large),
BorderWidth = 1,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
this.Content = new StackLayout
{
Children =
{
desknumber,
}
};
it doesnt work in any variable type so i stuck here i cant give a variable name to the button
every time you assign Content it overwrites what is already there
var sl = new StackLayout();
// some loop conditions
for(;;)
{
var button = new Button { ... };
sl.Children.Add(button);
}
this.Content = sl;

Xamarin - binding to variable in datatemplate

How do I bind a value from my object property to a variable so that I can pass it as an argument/parameter for my method "setFavoriteProduct(id_product, favnumber) which is triggered by my button "choiceButton". I require the property value "id_product".
When I bind it to Label "id_productLabel" then I can see it in the View just fine but I need to use the string value in my "setFavoriteProduct" function.
listView.ItemTemplate = new DataTemplate(() =>
{
Image productImage = new Image();
productImage.SetBinding(Image.SourceProperty, "imageURL");
//productImage.HeightRequest = 60;
productImage.HeightRequest = 120;
//productImage.WidthRequest = 60;
productImage.WidthRequest = 120;
productImage.Aspect = Aspect.AspectFit;
Label nameLabel = new Label();
nameLabel.SetBinding(Label.TextProperty, "name");
Label id_productLabel = new Label();
id_productLabel.SetBinding(Label.TextProperty, "id_product");
id_productLabel.IsVisible = true;
Button choiceButton = new Button();
choiceButton.Text = "Vali";
choiceButton.Clicked += (sender, args) => setFavoriteProduct(
id_product,
favnumber
);
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 5),
Orientation = StackOrientation.Horizontal,
Children = { productImage, id_productLabel , nameLabel, choiceButton }
}
};
the simplest way to do this is to get the BindingContext from the sender (button) and then get the property from that
choiceButton.Clicked += (sender, args) => {
var context = (MyClass)((Button)sender).BindingContext;
var id = context.id_product;
setFavoriteProduct(id,favnumber);
};
alternately, you could use the Button's Command and CommandParameter properties instead of the Clicked event

Windows CE datagrid button cell

I have a question. I have a datagrid and I want to add a button for deleting a row. Is is possible to display a button in datagrid. I'm running this program on windows ce operating system. I try to use it like this;
var f = shelfControlList.ToList();
List<ShelfControl> sortedProductList = new List<ShelfControl>();
sortedProductList.AddRange(f);
dataTable = new DataTable("InBox");
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Publisher");
dataTable.Columns.Add("ButtonCell");
dataGrid1.PreferredRowHeight = 30;
foreach (var sp in sortedProductList)
{
Button deleteButton = new Button();
deleteButton.Text = "Delete";
deleteButton.BackColor = Color.Red;
deleteButton.Width = 30;
this.btnDelete.Click += new System.EventHandler(this.deleteButton_Click_1);
this.Controls.Add(deleteButton);
dataTable.Rows.Add(sp.name, sp.publisher, deleteButton);
}
dataGrid1.DataSource = dataTable;
Thanks in advance.

xamarin forms custom control

I have a custom control, basically a stacklayout that I can bind a concatenated string, and have this break up into multiple objects, like a tag list.
public void Render()
{
if (ItemsSource == null)
return;
this.Orientation = Orientation == StackOrientation.Vertical ? StackOrientation.Vertical : StackOrientation.Horizontal;
this.Margin = new Thickness (0, 0, 0, 0);
List<string> items = ItemsSource.Split('|').ToList();
foreach (var item in items)
{
var frame = new Frame();
frame.BindingContext = item;
frame.BackgroundColor = Color.FromHex("#FCFACF");
frame.OutlineColor = Color.FromHex("#D0C0AD");
frame.HasShadow = false;
frame.Padding = new Thickness(4, 2, 4, 0);
var label = new Label();
label.Text = "{Binding}";
label.FontSize = 10;
label.Parent = frame;
Children.Add(frame);
}
}
but I can't seem to get this right, how do I add a lable to be a child of a frame, and how can I fix my binding, as at present if I add the label to the children of the stacklayout the text of the labels is {binding} and not the actual text.
Can someone please help me with this. I had all of this working if I added a ItemTemplate with a datatemplate and viewcell in the XAML, but I don't want this done in XAML as I'd like to reuse all of this in other views.
Frame has Content property
Frame.Content = label;
To assign a binding in code, use
label.SetBinding(Label.TextProperty, new Binding("."));

An error is occur when i called Web browser....

for (int i = 0; i < list.Count; i++) {
ds = new Discription();
PivotItem pivotItem = new PivotItem();
pivotItem.Header = list.ElementAt(i).header.ToString();
StackPanel sta = new StackPanel();
WebBrowser wb = new WebBrowser();
sta.Children.Add(wb);
pivotItem.Content = sta;
Pivot_item1.Items.Add(pivotItem);
wb.NavigateToString(list.ElementAt(i).Detail.ToString());
}
an error is occur when calling web browser control
You cannot call WebBrowser methods until it is in the visual tree.
Subscribe to the Loaded event of the Webbrowser control and move your navigation code to the loaded handler.
Replace the line
wb.NavigateToString(list.ElementAt(i).Detail.ToString());
with
var address = list.ElementAt(i).Detail.ToString();
wb.Loaded += (sender, e) => { wb.NavigateToString(address); }
for (int i = 0; i < list.Count; i++) {
ds = new Discription();
PivotItem pivotItem = new PivotItem();
pivotItem.Header = list.ElementAt(i).header.ToString();
Grid sta = new Grid();
WebBrowser wb = new WebBrowser();
var address = list.ElementAt(i).Detail.ToString();
wb.Loaded += (sender, e) => { wb.NavigateToString(address); };
sta.Children.Add(wb);
pivotItem.Content = sta;
Pivot_item1.Items.Add(pivotItem);
wb.NavigateToString(list.ElementAt(i).Detail.ToString());
}

Resources