WPF use touch for RadioButton - wpf

I'm using RadioButton on a WPF/C# on a parameter view. I want the user to choose only 1 option, so I use RadioButton and GroupName. It works fine, but it only react to click event, not touch...
Is there a way to have touch event to control my buttons or do I have to redefine all the default comportment of a RadioButton with touchDown event ?
Here are my radioButtons, even if I don't think they are usefull for my problem
<Viewbox Height="50">
<RadioButton x:Name="imposerOrdreHaut" Content="Avec" IsChecked="True" GroupName="ordre" Padding="2" Checked="RadioButtonChecked" />
</Viewbox>
<Viewbox Height="50">
<RadioButton Content="Sans" GroupName="ordre" Padding="2" Checked="RadioButtonChecked" />

Finally I did it using a touchDown event who switch "IsChecked=true", and it automatically switch false other buttons

Related

How can I close a popup in WPF from inside its user control

I have implemented a toggle button with a popup similar to what is described here. My popup displays a user control that is basically a custom menu with several images/buttons on it. Here is the code for my toggle button:
<ToggleButton x:Name="btnMenu" Background="Transparent" HorizontalAlignment="Right">
<StackPanel>
<Image Width="60" Height="60" Margin="10,0,10,0" Source="{StaticResource icon-menu}"/>
<Popup StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=btnMenu}" Placement="Bottom">
<controls:Menu/>
</Popup>
</StackPanel>
</ToggleButton>
Everything works fine, except that when I click on a button (which is bound to a command) in my user control, I would like the popup to disappear. Is there a way, through xaml, that I can close the parent popup when a button on the user control is pressed?
I did manage to find solutions on codeproject which would most likely solve the problem, and I also found various solutions on StackOverflow where all the xaml was inside the popup instead of a separate user control. I wanted to see if there was another preferred method (hopefully xaml based) that could solve this particular problem, and still let me keep my menu in a separate user control.

WP8 LongListSelector firing SelectionChanged event when CheckBox is clicked

I have a simple data template for the new Windows Phone 8 LongListSelector as follows:
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,-6,0,-10">
<CheckBox x:Name="ToDoCheckBox" Margin="0" IsChecked="{Binding ItemIsComplete}" Checked="ToDoCheckBox_Checked"/>
<TextBlock Text="{Binding ItemName}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
</StackPanel>
</DataTemplate>
There are suppsed to be a few more items in the template, that's why CheckBox's content property hasn't been used.
Now, I have page navigation implemented on SelectionChanged event. Problem is that, SelectionChanged even is fired even when the CheckBox is clicked. Earlier questions seem to discuss exactly opposite issue. I do not want SelectionChanged to be fired. Or, at least I don't want page to navigate on CheckBox events. I just want to have checkbox checked or unchecked event. How do I achieve that?
Selection is a bad way to trigger navigation. Without digging into that, you can avoid this by triggering the navigation when the other item in the template is tapped.
e.g.
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,-6,0,-10">
<CheckBox x:Name="ToDoCheckBox" Margin="0" IsChecked="{Binding ItemIsComplete}" Checked="ToDoCheckBox_Checked"/>
<TextBlock Text="{Binding ItemName}"
TextWrapping="NoWrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
Tap="TriggerNavigationToThisItem"/>
</StackPanel>
</DataTemplate>
The above assumes you have an event called TriggerNavigationToThisItem which will trigger the navigation. You could also add a command on the Item and bind to that to trigger the navigation.
If you have a more complex template you could encapsulate them in a container (like a Grid) and then have that trigger the navigation.
You can check the original source of en every selection changed event. If it is the checkbox - simply skip execution.
Also. Don't use native "LongListSelector.Selected" property and selection event at all. It has completely no scalability (for example for multiselection). Implement it with your own Tap event handling.

How to open pop-up from link button and close it using button inside the pop-up?

I have main window, containing grid with different user controls. Each control is defined in separate Xaml. Then in MainWindow.xaml i match all particles of my main window.
One of my userControls is a menu, containing buttons.
i want to call popup after clicking some button in menu. Popup will contain some text, buttons and will overlay the main window.
So the question is how to call pop-up from menu-button and close it from button located on pop-up?
Thanks!
You can put a togglebutton in your menu, then bind the IsOpen property of the Popup to the IsChecked property of the togglebutton.
<ToggleButton x:Name="toggle" Content="ToggleButton" HorizontalAlignment="Left" Height="68" Margin="78,104,0,0" VerticalAlignment="Top" Width="102"/>
<Popup IsOpen="{Binding ElementName=toggle, Path=IsChecked}" StaysOpen="False">
<Grid Background="#FFE5E5E5" Height="299" Width="269">
<ToggleButton IsChecked="{Binding ElementName=toggle, Path=IsChecked}" Content="Close" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
</Grid>
</Popup>
Change StaysOpen back to True (the default) if you don't want the popup to go away when you click off it. In this situation, the togglebutton inside the popup will look a bit strange by default, but you can style it to look better.
If not all your code is in the same Xaml file, instead of binding by ElementName, you can bind them all to a boolean property in your viewmodel.

WP7 - Image Click Event?

Is there a way to assign a click event to images? I would like to assign events to the delete and search buttons inside of my listbox that displays my data. Is there a way to do this using the image control or do I have to create a style in BLEND for a button?
<ListBox x:Name="lbPills" ItemsSource="{Binding pillItemsCollection}" SelectionChanged="lbPills_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,0">*</TextBlock>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding Name}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
<Image Source="Images/delete.png" Margin="10,0"/>
<Image Source="Images/search.png" Margin="10,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
To my knowledge there are no listeners on the Image itself for click and gesture events (they will have to be attached via Gestures as previously mentioned). One way to approach this is to re-template the button:
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="Images/delete.png" Margin="10,0"/>
</ControlTemplate>
</Button.Template>
</Button>
In setting the template on the button you will override the default template used by the phone (which has the extra padding, thick border, etc.). Using this method will allow you to tie into the button click event.
You can use the gesture listener to detect tap (click) events. A walkthrough here.
WP7 Tip of the Day: Silverlight Toolkit: Gestures
Alternatively, you can place your image into a Button control and retemplate it in blend to have the appearance you want.
Handle the ManipulationCompleted event (which is any tap, double-tap, swipe, caress or fondle) to your image(s). So:
<Image Source="Images/delete.png" Margin="10,0"/> becomes <Image x:Name="ImageDelete" ManipulationCompleted="ImageDelete_ManipulationCompleted" Source="Images/delete.png" Margin="10,0"/>. Then in the ImageDelete_ManipulationCompleted handler, track from whence it came in from the sender and do your thing.
If you want to only track a swipe instead of a tap, just do an if statement on the e.IsInertial from ManipulationCompletedEventArgs.
If your ListBox is not in a Panorama control, then you could just handle the SelectionChanged event for the list box and then display the Delete and Search options in an ApplicationBar for the page.
I do it with MouseLeftButtonDown and MouseLeftButtonUp. It replaces the tab or click on the device like you have click event on pc with mouse. It works in my app. Just try this and you will be happy, I guess.
It worked this way for me (Making Padding="-10" removed the button border and padding inside the button)
<Button x:Name="Channells" Click="Thumb_Click" Padding="-10" >
<Image
VerticalAlignment="Center"
Source="Assets/Images/thumb2.jpg"/>
</Button>
*.cs
void Thumb_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Thumb Clicked");
}

WPF Rectangle does not have a Click event

It seems that the WPF Rectangle shape does not have the Click event defined. What am I supposed to use instead?
It does have MouseUp, but it's not quite the same behavior.
If you're not happy with MouseDown and MouseUp, perhaps you could just put the Rectangle in a Button and handle the Button's Click event?
<Button>
<Button.Template>
<ControlTemplate>
<Rectangle .../>
</ControlTemplate>
</Button.Template>
</Button>
It really depends with the behavior you're after. Please elaborate if needs be.
To add click handing to a Rectangle itself, you can use the InputBindings property:
<Rectangle Fill="Blue" Stroke="Black">
<Rectangle.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
</Rectangle.InputBindings>
</Rectangle>
This will cause the FooCommand to be executed when the rectangle is clicked. Handy if you're using MVVM!
I was looking for the related DoubleClick event and came across this suggestion to simply embed the object of interest in a ContentControl.
Here is their example (with a border, which also did not support click/double click).
<ContentControl MouseDoubleClick="OnDoubleClick">
<Border Margin="10" BorderBrush="Black" BorderThickness="2">
<Grid Margin="4">
<Rectangle Fill="Red" />
<TextBlock Text="Hello" FontSize="15" />
</Grid>
</Border>
</ContentControl>
Rectangle has event Tapped, which works fine. In designing universal apps for Windows 8.1, there are new events. Tapped, DoubleTaped, RightTapped and Holding.

Resources