In WP8, they forgot to provide SelectedItem as a dependency property, hence I'm not able to bind to it. I fixed that using this: http://dotnet-redzone.blogspot.com/2012/11/windows-phone-8longlistselector.html
On doing so, I'm noticing that I'm not able to reset the property from the ViewModel, i.e. if I set the item to null in the ViewModel, it does not impact the UI. I have already provided two way binding in the UI but still setting the item to null in the ViewModel does not change the selected item in the LongListSelector. I also don't want to use the SelectionChanged event as I'm sharing ViewModels between WP7.5 app and a WP8 app, hence I want to push as much as I can into the ViewModel. Is there a solution for this?
It appears that the custom LongListSelector class that you are using does not handle the setter properly.
Replace the OnSelectedItemChanged callback with the following:
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selector = (LongListSelector)d;
selector.SetSelectedItem(e);
}
private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
{
base.SelectedItem = e.NewValue;
}
And there is full version of these two parts:
public class LongListSelector : Microsoft.Phone.Controls.LongListSelector
{
public LongListSelector()
{
SelectionChanged += LongListSelector_SelectionChanged;
}
void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItem = base.SelectedItem;
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(
"SelectedItem",
typeof(object),
typeof(LongListSelector),
new PropertyMetadata(null, OnSelectedItemChanged)
);
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selector = (LongListSelector)d;
selector.SetSelectedItem(e);
}
private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
{
base.SelectedItem = e.NewValue;
}
public new object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
}
Related
I have tab pages implementing different views, but I cannot initialize each of the tabs when navigating.
<TabbedPage.Children>
<tabPages:Page1/>
<tabPages:Page2/>
<tabPages:Page3/>
</TabbedPage.Children>
So what I did was to use IActiveAware as prism documentation suggested to know which tab page is currently active. So I have this class:
public abstract class TabbedChildViewModelBase : BaseViewModel, IActiveAware, INavigationAware, IDestructible
protected bool IsInitalized { get; set; }
private bool _IsActive;
public bool IsActive
{
get
{
return _IsActive;
}
set
{
SetProperty(ref _IsActive, value, RaiseIsActiveChanged);
}
}
public event EventHandler IsActiveChanged;
public virtual void OnNavigatingTo(NavigationParameters parameters)
{
}
protected virtual void RaiseIsActiveChanged()
{
IsActiveChanged?.Invoke(this, EventArgs.Empty);
}
public virtual void Destroy()
{
}
}
So each child view models inherits the child view model base:
public class Page1 : TabbedChildViewModelBase
{
public CurrentSeaServiceViewModel()
{
IsActiveChanged += HandleIsActiveTrue;
IsActiveChanged += HandleIsActiveFalse;
}
private void HandleIsActiveTrue(object sender, EventArgs args)
{
if (IsActive == false)
{
TestLabelOnly = "Test";
}
// Handle Logic Here
}
private void HandleIsActiveFalse(object sender, EventArgs args)
{
if (IsActive == true) return;
// Handle Logic Here
}
public override void Destroy()
{
IsActiveChanged -= HandleIsActiveTrue;
IsActiveChanged -= HandleIsActiveFalse;
}
}
The problem is, the child vm isn't initializing. Is there something needed in order to implement IActiveAware properly nor launching the IsActive property
I still used IActiveAware unfortunately, to make the childtabbedviewmodel work you need to bind the page to its own view model.
So here's what I did:
<TabbedPage.Children>
<views:ChildPage1>
<views:ChildPage1.BindingContext>
<viewModels:ChildPage1ViewModel/>
</views:ChildPage1.BindingContext>
</views:ChildPage1>
<views:ChildPage2>
<views:ChildPage2.BindingContext>
<viewModels:ChildPage2ViewModel/>
</views:ChildPage2.BindingContext>
</views:ChildPage2>
</TabbedPage.Children>
I used the property BindingContext of my views and
using IActiveAware I would also know what tab is currently active. Hope anyone helps this who finds trouble binding the child pages of a tab.
I have a page called MapPage.xaml and a code behind called MapPage.xaml.cs. In my android project, I have another file called CustomMapRenderer.cs. In the CustomMapRenderer.cs file, I need to retrieve the item selected variable in a XAML picker found in my MapPage.xaml file, which changes when a user picks an option in my XAML picker.
How to I reference the picker from my CustomMapRenderer.cs?
In the CustomMapRenderer.cs file, I need to retrieve the item selected variable in a XAML picker found in my MapPage.xaml file, which changes when a user picks an option in my XAML picker.
If you followed the official doc Customizing a Map to create your CustomMapRenderer, then in PCL, there should be a class which inherits from Map, for example:
public class CustomMap : Map
{
}
Then, if your picker is another control in your MainPage, you can create a bindable property for your CustomMap, and override OnElementPropertyChanged in your renderer to get this property when it changed.
For example, in PCL:
public class MapWithMyZoomControl : Map
{
public ZoomState MyZoom
{
get { return (ZoomState)GetValue(MyZoomProperty); }
set { SetValue(MyZoomProperty, value); }
}
public static readonly BindableProperty MyZoomProperty =
BindableProperty.Create(
propertyName: "MyZoom",
returnType: typeof(ZoomState),
declaringType: typeof(MapWithMyZoomControl),
defaultValue: ZoomState.normal,
propertyChanged: OnZoomPropertyChanged);
public static void OnZoomPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
}
public enum ZoomState
{
normal,
zoomin,
zoomout
}
}
And in its renderer:
public class MapWithMyZoomControlRenderer : MapRenderer, IOnMapReadyCallback
{
private GoogleMap map;
public void OnMapReady(GoogleMap googleMap)
{
map = googleMap;
map.UiSettings.ZoomControlsEnabled = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
}
if (e.NewElement != null)
{
var formsMap = (MapWithMyZoomControl)e.NewElement;
((MapView)Control).GetMapAsync(this);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var element = Element as MapWithMyZoomControl;
if (e.PropertyName == "MyZoom" && map != null)
{
if (element.MyZoom == MapWithMyZoomControl.ZoomState.zoomin)
{
map.AnimateCamera(CameraUpdateFactory.ZoomIn());
}
else if (element.MyZoom == MapWithMyZoomControl.ZoomState.zoomout)
{
map.AnimateCamera(CameraUpdateFactory.ZoomOut());
}
element.MyZoom = MapWithMyZoomControl.ZoomState.normal;
}
}
}
Out of this map control, I use buttons to control to zoom the map:
map.MyZoom = MapWithMyZoomControl.ZoomState.zoomin;
It'a a demo, but you can modify it to make property connected to your picker.
I'm trying to select multiple items in a GridView by hovering over them with pressed mouse (like drawing). I tried to achieve this with the PointerEntered event but I'm unable to change the selction from code. Is there a way to implement a custom selection mode?
This didn't work for me because I can't use Style.Triggers in Win RT XAML:
https://stackoverflow.com/a/2886223/5739170
You will have to inherit the gridview control and override the PrepareContainerForItemOverride method:
The code:
public class MyGridView : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
(element as GridViewItem).PointerMoved += MyGridView_PointerMoved;
base.PrepareContainerForItemOverride(element, item);
}
private void MyGridView_PointerMoved(object sender, PointerRoutedEventArgs e)
{
//your logic for setting the isselected
(sender as GridViewItem).IsSelected = true;
}
}
This is how I finally implemented it based on Chirag Shah's answer:
class MyGridView : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
(element as GridViewItem).PointerEntered += SelectItemOnEntered;
(element as GridViewItem).AddHandler(PointerPressedEvent, new PointerEventHandler(SelectItemOnPressed), true);
base.PrepareContainerForItemOverride(element, item);
}
private void SelectItemOnPressed(object sender, PointerRoutedEventArgs e)
{
(sender as GridViewItem).IsSelected = !(sender as GridViewItem).IsSelected;
}
private void SelectItemOnEntered(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact)
(sender as GridViewItem).IsSelected = !(sender as GridViewItem).IsSelected;
}
}
I hope this helps everyone who wants to implement this selection mode.
I've been trying my hand at creating a simple user control with dependencyproperty and binding it, but it doesn't seem to work, not sure why. I'll just dive straight into code, please ignore the fact that the control doesn't make sense, it's just for illustrative purposes (written in WP8 if that matters).
My simple user control, it's basically a line with a property to turn it off or on.
<Grid x:Name="LayoutRoot" Background="Transparent">
<Line Height="105" Width="105" X2="100" Y2="100" Visibility="{Binding LineVisible}" Stroke="#FFFC1515" StrokeThickness="5"/>
</Grid>
public partial class SimpleUserControl : UserControl
{
public SimpleUserControl()
{
InitializeComponent();
DataContext = this;
}
public static readonly DependencyProperty LineVisibleProperty = DependencyProperty.Register("LineVisible", typeof(bool), typeof(SimpleUserControl), new PropertyMetadata(new PropertyChangedCallback(OnLineVisibleChanged)));
public bool LineVisible
{
get { return (bool)GetValue(LineVisibleProperty); }
set { SetValue(LineVisibleProperty, value); }
}
private static void OnLineVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
bool newvalue = (bool)e.NewValue;
Visibility vis = newvalue ? Visibility.Visible : Visibility.Collapsed;
(d as SimpleUserControl).Visibility = vis;
}
}
The test app
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<uc:SimpleUserControl LineVisible="{Binding class1.Vis}"/>
</Grid>
public partial class MainPage : PhoneApplicationPage
{
public Class1 class1 { get; set; }
public MainPage()
{
InitializeComponent();
DataContext = this;
}
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
class1 = new Class1() { Vis = false };
}
}
The class1 that it's bound to
public class Class1 : INotifyPropertyChanged
{
private bool _vis;
public bool Vis
{
get { return _vis; }
set
{
_vis = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Vis"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
It doesn't seem to work, however, if it is set explicitly like below it works.
<uc:SimpleUserControl LineVisible="False"/>
I'm sure it's something simple, but I'm not seeing it.
Thanks for any help.
The problem was I was setting the DataContext = this in the UserControl and when binding to Vis in the testapp, it would override and search for Vis in the UserControl (which of course does not exist there). I did see binding errors in the debug output window which confirms this. The solution was to set the LayoutRoot of the UserControl to this as was mentioned in the link I posted earlier.
The Visibility property of WPF controls does not use bool values, it requires the Visibility enum. Thus you have two options:
Change LineVisibiltyProperty to Visibility instead of bool.
Use a converter to bind to bool and convert to Visibility.
I would suggest using the second option as this in my opinion is the better solution.
This might be helpful.
I have a simple Silverlight 4 application and have added a child window to it. I am using the below code to open it on a button click. This seems like it should work, does it not?
public void btnAbout_Click(object sender, RoutedEventArgs e)
{
About aboutThis = new About();
aboutThis.Show();
}
The "About" class looks like this:
public partial class About : ChildWindow
{
public About()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
I don't see any reason why it should not work.
Samples:
http://www.tanguay.info/web/index.php?pg=codeExamples&id=135
http://www.silverlighttoys.com/Tutorials.aspx?tutorial=2
What is your XAML like?
Try setting the Width and Height to 600px by 600px of your About Childwindow from xaml.