Answered by:
Event after button command is raised

Question
-
Hello,
I have a UserControl (implementing popup window) where I have a textbox and button. I have a command binded to the button, which parameter is binded from the textbox's text property.
What I want to implement, is that after command is raised I want to clear textbox's text and also hide popup itself.
I solved second problem easily with creating Click event handler (doesn't depends if command is raised inside Collapsed view), but I can't clear textbox here, because event is raised before command.
So my question is, is there some event that is raised after command or how can I implement this behaviour ?
Thank you
Lukas
Xaml code
------------
<UserControl x:Class="MyApplication.CustomComponents.GetPlanNamePopup" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyApplication.CustomComponents" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="350" x:Name="ParentPopupWindow" xmlns:toolkit="using:WinRTXamlToolkit.Controls"> <Border BorderThickness="2" BorderBrush="Black" Background="White"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="2*" /> </Grid.RowDefinitions> <toolkit:WatermarkTextBox x:Name="PlanNameTextBox" Margin="10" Grid.Row="0" WatermarkText="Enter plan name..." x:Uid="EnterPlanName" Background="White" BorderThickness="2" BorderBrush="#5c5c5c" Height="30" FontSize="15" MaxLength="50" TextWrapping="NoWrap" TextAlignment="Left" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" KeyDown="TextBoxKeyDown"/> <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right"> <Button Margin="10" Visibility="Collapsed" Background="LightGray"> <TextBlock FontSize="15" Margin="5" FontWeight="Bold" Foreground="Black" x:Uid="CANCEL" Text="Cancel" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Button> <Button x:Name="SaveButton" Margin="10" Background="LightGray" Click="SaveButton_Click" Command="{Binding ElementName=ParentPopupWindow, Path=AddPlanCommand}" CommandParameter="{Binding ElementName=PlanNameTextBox, Path=Text}"> <TextBlock FontSize="15" Margin="5" FontWeight="Bold" Foreground="Black" x:Uid="ADD" Text="Save" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Button> </StackPanel> </Grid> </Border></UserControl>
Code behind
-----------
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.InteropServices.WindowsRuntime;using System.Windows.Input;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236namespace MyApplication.CustomComponents{ public sealed partial class GetPlanNamePopup : UserControl { public static readonly DependencyProperty AddPlanCommandProperty = DependencyProperty.Register("AddPlanCommand", typeof(ICommand), typeof(GetPlanNamePopup), new PropertyMetadata(null)); public ICommand AddPlanCommand { get { return GetValue(AddPlanCommandProperty) as ICommand; } set { SetValue(AddPlanCommandProperty, value); } } public GetPlanNamePopup() { this.InitializeComponent(); } public void ClearTextBox() { this.PlanNameTextBox.Text = String.Empty; } private void TextBoxKeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == Windows.System.VirtualKey.Enter) { e.Handled = true; // TODO: fake ok button click } } private void SaveButton_Click(object sender, RoutedEventArgs e) { this.Visibility = Windows.UI.Xaml.Visibility.Collapsed; } }}
Monday, June 2, 2014 8:58 PM
Answers
-
In my ViewModel, there is just DelegateCommand<string> and text is passed by argument.
<toolkit:WatermarkTextBox
x:Name="PlanNameTextBox"
Margin="10" Grid.Row="0"
WatermarkText="Enter plan name..." x:Uid="EnterPlanName"
Background="White" BorderThickness="2" BorderBrush="#5c5c5c"
Height="30" FontSize="15" MaxLength="50" TextWrapping="NoWrap" TextAlignment="Left"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Top"
KeyDown="TextBoxKeyDown"/><Button x:Name="SaveButton" Margin="10" Background="LightGray" Click="SaveButton_Click"
Command="{Binding ElementName=ParentPopupWindow, Path=AddPlanCommand}"
CommandParameter="{Binding ElementName=PlanNameTextBox, Path=Text}">Problem is, that I don't know which event use or something to clear it after command is raised and I can't use click event, because it's raised before command.
EDIT:
I have added TextBlock which is always Collapsed. In the Click event, I copy Text from TextBox to the TextBlock. Command Argument parameter is binded from the hidden TextBlock.Text.
If someone would found better solution, I would appreciate it.- Edited by Lukáš Vavrek Tuesday, June 3, 2014 5:26 AM Possible solution
- Marked as answer by Lukáš Vavrek Tuesday, June 3, 2014 6:53 AM
Tuesday, June 3, 2014 5:17 AM
All replies
-
Is the text parameter and command in the ViewModel? If so, clearing the text in the viewmodel will also clear the text in the view through data binding.
As a second option, send the textbox as a command parameter.
Something like:
<Textbox x:Name="Mytb" Text="{Binding text}"/>
<Button Command="{Binding YourCommand}" CommandParameter="{Binding ElementName=Mytb}" />
- Edited by Bryan Stump Monday, June 2, 2014 10:00 PM
Monday, June 2, 2014 9:58 PM -
In my ViewModel, there is just DelegateCommand<string> and text is passed by argument.
<toolkit:WatermarkTextBox
x:Name="PlanNameTextBox"
Margin="10" Grid.Row="0"
WatermarkText="Enter plan name..." x:Uid="EnterPlanName"
Background="White" BorderThickness="2" BorderBrush="#5c5c5c"
Height="30" FontSize="15" MaxLength="50" TextWrapping="NoWrap" TextAlignment="Left"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Top"
KeyDown="TextBoxKeyDown"/><Button x:Name="SaveButton" Margin="10" Background="LightGray" Click="SaveButton_Click"
Command="{Binding ElementName=ParentPopupWindow, Path=AddPlanCommand}"
CommandParameter="{Binding ElementName=PlanNameTextBox, Path=Text}">Problem is, that I don't know which event use or something to clear it after command is raised and I can't use click event, because it's raised before command.
EDIT:
I have added TextBlock which is always Collapsed. In the Click event, I copy Text from TextBox to the TextBlock. Command Argument parameter is binded from the hidden TextBlock.Text.
If someone would found better solution, I would appreciate it.- Edited by Lukáš Vavrek Tuesday, June 3, 2014 5:26 AM Possible solution
- Marked as answer by Lukáš Vavrek Tuesday, June 3, 2014 6:53 AM
Tuesday, June 3, 2014 5:17 AM