Silverlight Listbox: Binding does not get updated - data-binding

I have a Wizard control that has multiple steps. Controls that are not visible get removed from the visual tree. I have a ListBox on one page, that binds to an ObservableCollection<T>. When items get added or removed to that ListBoxon one page, the ListBox on another page (with the same ItemsSource), the binding on the other page does not get updated. I hope this explains my problem clearly enough.
How do I get this binding to update when the page gets added to the visual tree again ?

I cannot reproduce your problem. I was able to remove a ListBox from the visual tree, add objects to the ObservableCollection, and when I add it to the visual tree, items are actually updated.
Try working around your problem by setting visibility to Collapsed rather than removing from Visual Tree.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.listBox1.ItemsSource = o;
this.listBox2.ItemsSource = o;
}
ObservableCollection<int> o = new ObservableCollection<int>();
private void buttonList1_Click(object sender, RoutedEventArgs e)
{
if (this.listBox1.Parent == null)
this.LayoutRoot.Children.Add(this.listBox1);
else
this.LayoutRoot.Children.Remove(this.listBox1);
//this.listBox1.Visibility = this.listBox1.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
private void buttonList2_Click(object sender, RoutedEventArgs e)
{
if (this.listBox2.Parent == null)
this.LayoutRoot.Children.Add(this.listBox2);
else
this.LayoutRoot.Children.Remove(this.listBox2);
//this.listBox2.Visibility = this.listBox2.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
private void ButtonAddMore_Click(object sender, RoutedEventArgs e)
{
o.Add(o.Count);
}
}

Related

How to use PKPaymentButton in Xamarin.Forms

I have a page in Xamarin.Forms in which I have to show PKPaymentButton by using the same PKPaymentButton class which is a child of UIButton in PassKit.
I have written a custom ButtonRenderer and trying to convert the button into PKPayment button.
I got so far that in custom renderer we can change the appearance of a button but can we use something like creating a new button instance in my case PKPaymentButton and replace it with Button.
UPDATE-
I have achieved this by-
public class ApplePayButtonRenderer : Xamarin.Forms.Platform.iOS.ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
var button = new PKPaymentButton(PKPaymentButtonType.Buy, PKPaymentButtonStyle.Black);
SetNativeControl(button);
}
}
}
Now I am trying to get its click into Xamarin.Forms
You could use Messaging Center to send message when you click the payment Button.
in Custom Renderer
var button = new PKPaymentButton(PKPaymentButtonType.Buy, PKPaymentButtonStyle.Black);
button.TouchUpInside += Button_TouchUpInside;
SetNativeControl(button);
private void Button_TouchUpInside(object sender, EventArgs e)
{
MessagingCenter.Send<Object>(this,"PayButtonClick");
}
in Forms
Add the following code to the constructor of the ContentPage which contains the Payment Button
public xxxPage()
{
InitializeComponent();
MessagingCenter.Subscribe<Object>(this, "PayButtonClick",(args)=> {
//do some thing you want
});
}

how to extend all contentpages using pagerenderer in UWP?

I would like to extend all the contentpages in my xamarin.forms app with a native view in UWP. I can basically go to each and every page and embed a native view but i dont want this. I want to know if there is a way to do it using a pagerenderer. I tried doing like below.
my idea was to get current page rendering and extend the content with native view and stacklayout and define app.content again with this change. It works in general. If you run the small test project below, you can see that native UWP FontIcons are displayed for each page but there is a problem, if i navigate same page 2 times in MasterDetail in the attached project, page becomes blank. Why is this happening?
and is the approach below best for my case? I am open for alternative solutions.
[assembly: ExportRenderer(typeof(ContentPage), typeof(App3.UWP.ContentPageRenderer))]
namespace App3.UWP
{
public class ContentPageRenderer : PageRenderer
{
bool isDisposing = false;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
{
base.OnElementChanged(e);
if (isDisposing)
return;
if (e.OldElement != null || Element == null)
{
return;
}
ContentPage page = ((ContentPage)Element);
if (page.Content == null)
return;
var XboxControls = new MyUserControl1();
StackLayout stackLayout = new StackLayout() { Orientation = StackOrientation.Vertical };
stackLayout.Children.Add(page.Content);
stackLayout.Children.Add(XboxControls.ToView());
page.Content = stackLayout;
}
protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Size finalSize)
{
return base.ArrangeOverride(finalSize);
}
protected override void Dispose(bool disposing)
{
isDisposing = disposing;
base.Dispose(disposing);
}
}
Test Project

Rearranging parent-child activation order in Caliburn Micro

During my override of OnActivate() in my view-model, I need to call GetView() in order to focus an element. When I do this after I have previously activated my view, it's fine. But when I call this the first activation, it fails.
I was able to get it to work by swapping a few lines in ConductorBaseWithActiveItem.ChangeActiveItem. The original is as follows:
protected virtual void ChangeActiveItem(T newItem, bool closePrevious) {
ScreenExtensions.TryDeactivate(activeItem, closePrevious);
newItem = EnsureItem(newItem);
if(IsActive)
ScreenExtensions.TryActivate(newItem);
activeItem = newItem;
NotifyOfPropertyChange("ActiveItem");
OnActivationProcessed(activeItem, true);
}
and with my changes:
protected virtual void ChangeActiveItem(T newItem, bool closePrevious) {
ScreenExtensions.TryDeactivate(activeItem, closePrevious);
newItem = EnsureItem(newItem);
activeItem = newItem;
NotifyOfPropertyChange("ActiveItem");
if (IsActive)
ScreenExtensions.TryActivate(newItem);
OnActivationProcessed(activeItem, true);
}
This seems to work. Notifying that "ActiveItem" changed triggers the code to load and cache the view. Then ScreenExtensions.TryActivate calls my OnActivate override.
Question: I haven't noticed any problems doing this, but I'm curious if anyone knows better than I do what repercussions this change could have?
Thanks!
One thing you could try is overriding Caliburn's OnViewAttached method and trying to focus it there. That being said, in MVVM, focus is more of a View concern, so if possible, that logic should be moved from the ViewModel to the View.
One way you may be able to solve this is by creating an attached behavior (you will need a reference to the Microsoft.Expression.Interactions assembly):
public class FocusWhenVisibleBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += this.Loaded;
this.AssociatedObject.IsVisibleChanged += this.VisibleChanged;
}
protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= this.Loaded;
this.AssociatedObject.IsVisibleChanged -= this.VisibleChanged;
}
private void Loaded(object sender, RoutedEventArgs e)
{
this.TryFocus();
}
private void VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.TryFocus();
}
private void TryFocus()
{
if (this.AssociatedObject.IsLoaded && this.AssociatedObject.IsVisible)
{
// Focus the control
this.AssociatedObject.Focus();
}
}
}
And that attaching that behavior to whatever control you want to focus:
<Button>
<i:Interaction.Behaviors>
<b:FocusWhenVisibleBehavior/>
</i:Interaction.Behaviors>
</Button>

Get e parameter from a button that hasn't been clicked

Is there a way that I could access the e event arguments for a button that has not been clicked?
I need to delete multiple entries in a gridview by clicking a button and having it simulate clicking the delete button for each selected entry, but I can't use performClick, so I'm trying to call the actual method that deletes each one. However, that method requires an "e As System.Web.UI.WebControls.GridViewCommandEventArgs" parameter and I can't figure out how to get that.
You won't be able to access the EventArgs parameter.
I'd suggest you design your code like this:
public class MyClass
{
private ListView listView;
protected void OnClick(EventArgs e)
{
performAction();
}
private void performAction()
{
listView.deleteSelectedItems();
}
}
Don't implement functionality you are going to need somewhere else in delegates. Instead call this functionality inside the delegates' body. This way you can reuse performAction() somewhere else ..
Your problem calling delete button can be resolved if you add one check box in each row of datagrid and on click of button Delete you can perform delete operation for the checked rows in following manner
Protected void btnDelete_Click(object sender, EventArgs e)
{
for(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if(chkDelete != null)
{
if(chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
ids.Add(strID); //ids can colletion of any type
}
}
}
}
Now send ids to any function to perform delete.

ViewState error with dynamically built menu

I have a Menu control that I dynamically populate with categories. When a user clicks a category, the postback should populate a grid with products in that category. However, I seem to have tried every way possible, i.e. whatever page life cycle time, only to always get this error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.
Here is all my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildCategoryMenu();
}
}
protected void categoryMenu_ItemClick(object source, DevExpress.Web.ASPxMenu.MenuItemEventArgs e)
{
var catId = new Guid(e.Item.Name);
ListProductsByCatId(catId);
}
private void BuildCategoryMenu()
{
var cats = _categoryService.ListActive();
categoryMenu.Items.Clear();
foreach (var cat in cats)
{
categoryMenu.Items.Add(new MenuItem { Text = cat.Name, Value = cat.id.ToString() });
}
}
private void ListProductsByCatId(Guid catId)
{
productGrid.DataSource = _productService.ListByCatId(new Guid("a5c2f0ef-a3cc-4af1-abac-37f1be6a5c74"));
productGrid.DataBind();
}
Here is my Menu:
<asp:Menu ID="categoryMenu" runat="server" EnableViewState="false">
</asp:Menu>
EnableViewState is only false because it didn't work with true either.
One answer to this is to avoid the postbacks and code URL's into the menu items. More RESTful, but probably making AJAXing the menu a bit harder.

Resources