I want to have a stepper to increase/decrease the value in an Entry. I referenced to other posts online but my increment does not work. Do I need to have a code behind for the values to increase in Entry?
xaml
<Entry BindingContext="{x:Reference stepper}" Text="1" />
<Stepper x:Name="stepper" Minimum="1" Maximum="4" Increment="1"/>
you need to bind the Entry's Text property to the Stepper's value property
<Entry BindingContext="{x:Reference stepper}" Text="{Binding Value}" />
<Stepper x:Name="stepper" Minimum="1" Maximum="4" Increment="1"/>
do you need write the value of stepper on the entry? if that right then you need create a binding value, because yo need "listening" when the value change and put this value on the entry.
here there is an explain for this.
Related
I have a custom behavior to validate telephonenumbers in my view attached to a entry:
<Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
<Entry.Behaviors>
<behaviors:TelNumBehavior x:Name="NumValidatorUser" />
</Entry.Behaviors>
</Entry>
I am using the mvvm pattern with view and viewmodel.
The behavior has a bindable isValid property. How can I use that value in my Viewmodel? How can these two classes communicate? The messaging-service is not an option for me, because I have multiple behaviors and I need to validate them all. Is there a way to access the isValid-att of the behaviors?
Name your page, to be referenced later on:
<ContentPage x:Name="Root" etc, etc>
in your behavior set the path and source to the page's binding-context:
<Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
<Entry.Behaviors>
<behaviors:TelNumBehavior x:Name="NumValidatorUser" />
IsValid="{Binding Source={x:Reference Root},
Path=BindingContext.YourPropertyIsValid, Mode=TwoWay}"/>
</Entry.Behaviors>
</Entry>
My code has:
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Button.FontSize>
<OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
</Button>
Can someone explain what x:Double means
I have broken it down for you with comments in the XAML.
<!-- Here is the button declared, note how you also see the x:Name here -->
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<!-- Besides giving properties to a control, in this case a button, as attributes, you can also set them by adding them as child nodes. That is what happens with the FontSize here -->
<Button.FontSize>
<!-- We are not just setting the FontSize here, we are also letting the value be dependent on which platform we are running on. For iOS the value will be 25, for Android 20. -->
<OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
</Button>
Now, the x:Double is needed to tell the OnPlatform tag which type we should provide to it, this can also be x:Int32 or any other type if needed. Because we will always provide string values in the OnPlatform tag, it needs to know to what type it has to cast the value.
Remember that I pointed out to you also the x:Name property. the x is a shorthand for the namespace where to find the type. If you look at your page declaration, it will probably have an attribute like: xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml", see how the x is declared here? That is why the x is needed in front, to tell the XAML that the Double type can be found in the http://schemas.microsoft.com/winfx/2009/xaml namespace, abbreviated to x by default.
Note that the OnPlatform tag in this way is deprecated as of Xamarin.Forms 2.3.4. You should now use it as such:
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Button.FontSize>
<OnPlatform x:TypeArguments="x:Double" x:Key="WindowBackgroundTable">
<On Platform="Android" Value="20" />
<On Platform="iOS" Value="25" />
</OnPlatform>
</Button.FontSize>
</Button>
Sorry for the poor english.
When used in XAML this tag needs the type of the value you're sending to the property, once this is a generic method. I belive that XF uses reflection and castings to set the attribute to the specific platform.
See more in this article.
I hope it helps you.
I'd like to add a tap gesture to the Picker - so when a value is selected (double tapped) I can call a method. I have tried the following. But it never fires! Any ideas?
<Picker Grid.Row="5" Margin="0,0,100,0" Title=" -Select State" SelectedIndex="{Binding StatesSelectedIndex, Mode=TwoWay}" ItemsSource="{Binding PPStates}" ItemDisplayBinding="{Binding Path=[display_name]}">
<Picker.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OnTapPicker}"
NumberOfTapsRequired="1" />
</Picker.GestureRecognizers>
</Picker>
I have tired the sample code in
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/
By adding this Tapped="OnTapGestureRecognizerTapped"
I don't know how you could do that in the way you desire, but you could fire the selectedindexchanged. If I'm right this should fire when the user selected one of the 50 states. Here you have some documentation about the picker.
https://developer.xamarin.com/api/type/Xamarin.Forms.Picker
If you want to use a picker that is bindable you should use this:
https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker
let me know if this helped you.
Edit:
Pickers are bindable by default nowadays
how to pick up the values of a listview done in WindowsPhone 8.1 then click on a MenuFlyoutItem. Example I have a listview with id, name and telephone after holding the list to appear MenuFlyoutItem want to click on a menu option and know the values of the id, name and telephone number of the line selected.
I think that this question was answered many times but I will try to give you a solution.
You can add flyout to ItemTemplate of your ListView, like this:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Delete" />
<MenuFlyoutItem Text="Refresh" />
<MenuFlyoutItem Text="Share" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
Now, MenuFlyoutItem has event named "Click" so if you are using a "code-behind" approach you can create event handlers for your click events of menuFlyoutItems in page code. I believe that event args of this event will have information about which item was clicked.
Please look at this blog note I found, it may be useful for you:
http://igrali.com/2014/04/28/show-context-menu-using-menuflyout-windows-phone-8-1/
I have customized the button in this way:
<Button BorderBrush="Transparent" Name="DialButton" Click="DialButton_Click" >
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="43" Name="lblNumber" Margin="0,-5,0,0" />
<TextBlock FontSize="12" Margin="5,20,0,0" Name="lblCharacter" />
</StackPanel>
</Button>
Now, when a user presses the button I want the OnPress state to change the color of the labels. I can do this if it's a simple button by changing the Pressed state. But my label is placed inside a stack panel. How can I change the color in this case? Or in which event can I change the colors of the labels from C#.
You can use the PropertyChangeAction in cases like this. You can find this in the behaviors category on the Assets tab in Expression Blend.
Apply this action on the labels. Change the trigger property to the DataTrigger instead of the default EventTrigger. Bind the trigger to the IsPressed property of the DialButton. Add two PropertyChangeActions per TextBlock and set the Value for one of the to true and the other one to false.
Here's an example for one of them. The other is exactly the same.
<TextBlock FontSize="43" x:Name="lblNumber" Margin="0,-5,0,0" Text="25">
<i:Interaction.Triggers>
<ec:DataTrigger Binding="{Binding IsPressed, ElementName=DialButton}" Value="true">
<ec:ChangePropertyAction PropertyName="Foreground">
<ec:ChangePropertyAction.Value>
<SolidColorBrush Color="Red"/>
</ec:ChangePropertyAction.Value>
</ec:ChangePropertyAction>
</ec:DataTrigger>
<ec:DataTrigger Binding="{Binding IsPressed, ElementName=DialButton}" Value="false">
<ec:ChangePropertyAction PropertyName="Foreground">
<ec:ChangePropertyAction.Value>
<SolidColorBrush Color="{StaticResource PhoneForegroundColor}"/>
</ec:ChangePropertyAction.Value>
</ec:ChangePropertyAction>
</ec:DataTrigger>
</i:Interaction.Triggers>
</TextBlock>
If the i: or ec: isn't working, make sure you've got these lines at the top of your xaml file.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
You'll need to turn this into a custom control and then you can manage the styling of each component based on the state.
Try this in the click event of button
Button butClicked = (Button)sender;
StackPanel panel1 = (StackPanel)butClicked.Content;
var child1Panel1 = panel1.Children[0] as TextBlock;
child1Panel1.Foreground = new SolidColorBrush(Color.FromArgb(255, 18, 18, 18));
If you're only going to use this button once, probably the easiest way would be to open the .xaml file in Expression Blend, and use Blend to customize the button as you wish, including the state change. If you're using the button in more than one place, do as Matt suggested and make it a custom control (which you can also use Blend to design) that you can reuse.