How do i detect if the textbox has been tapped. User will normally tap on the textbox before they can type something. I tried textbox.tapped = true , .selected , it doesnt work for WinRT/Windows 8 metro application. Can someone help me out? THanks
It cannot be simpler. As dotNetNinja says you have to sign up for the Tapped event. Example follows, notice TextBox_Tapped method in the xaml code and in the cs code behind.
.xaml code:
<Page x:Class="TappedEventExample.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TappedEventExample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
<TextBox
x:Name="MyTextBox"
HorizontalAlignment="Left"
Margin="100,100,0,0"
TextWrapping="Wrap"
Text="My TextBox"
VerticalAlignment="Top"
Height="100" Width="200"
Tapped="TextBox_Tapped"/>
</Grid>
</Page>
.xaml.cs code:
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void TextBox_Tapped(object sender, TappedRoutedEventArgs e)
{
this.MyTextBox.Text = "I was tapped.";
}
}
That's all.
You should be able to subscribe to the tapped event for the control. It is document here
Related
I really don't know how to search to find a solution to this (I googled a lot, maybe I'm blind...).
I have a ComboBox which also contains a TextBox. The ComboBox is instantiated in a separate Control.xaml with a specific DataContext, where it gets its content for the Popup list.
Now when I type something into the TextBox, I want to trigger a method which then filters the list of my DataContext for the specific elements.
So my ComboBox.cs has some of the following content:
public event EventHandler FilterTextChanged;
protected virtual void OnFilterTextChanged(EventArgs args)
{
FilterTextChanged?.Invoke(FilterText, args);
}
public string FilterText
{
get { return _filterText; }
set
{
//This point is reached when I type something into the TextBox within the ComboBox
if (_filterText == value) return;
_filterText = value;
OnFilterTextChanged(EventArgs.Empty);
OnPropertyChanged("FilterText");
}
}
And in my Control.xaml, I have configured it like this:
<my:ComboBox x:Name="FURecipeComboBox"
AuthorizationMode="IsEnabled"
IsTextSearchEnabled="False"
StaysOpenOnEdit="True"
FilterTextChanged="FURecipeComboBox_OnFilterTextChanged"
ItemsSource="{Binding RecipeFileNames}"
SelectedItem="{Binding Value, Delay=100, ElementName=AlphaNumericTouchpadTextVarIn}"
d:DataContext="{d:DesignInstance {x:Type adapter:ToolRecipeVariableInfo}, IsDesignTimeCreatable=False}">
</my:ComboBox>
But I just can't get it to catch the event "FilterTextChanged", and my method "FURecipeComboBox_OnFilterTextChanged" will not be reached anytime...
I would be really really glad for some hints or help!
Kind regards
BB
Have a look at RoutedEvents at microsoft docs
This is a example post from Stackoverflow
In your case try to change EventHandler to RoutedEventHandler.
I made a small example:
Main.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" Height="207" Margin="348,175,0,0" VerticalAlignment="Top" Width="311" MyClick="UserControl1_MyClick"/>
</Grid>
Main.cs
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UserControl1_MyClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("Yep");
}
}
}
control.cs
public partial class UserControl1 : UserControl
{
public event RoutedEventHandler MyClick;
public UserControl1()
{
InitializeComponent();
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
if (MyClick != null)
MyClick(this, new RoutedEventArgs());
}
}
}
control.xaml
<UserControl x:Class="WpfApp1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="372.313" d:DesignWidth="350">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="218" Margin="59,54,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="202" KeyDown="textBox_KeyDown"/>
</Grid>
I am having an strange issue with clicking a button. When I click on a button, it is not registered as a click unless I move the mouse before I release the left button. Below is what I used in a new program to verify I didn't do something in the program I was working on.
I am using a Raspberry Pi 2 and build version 10.0.16212.1000
xaml:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="175,216,0,0" VerticalAlignment="Top" Click="button_Click"/>
</Grid>
</Page>
c#:
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Button Clicked");
}
}
}
I have a WPF application where tooltip is bound to a viewmodel, and all is great when I hover over my control and wait for the tooltip.
To support mouseless devices, I would like to show the same tooltip, when "clicking" my control.
If I implement a MouseDown handler that sets the tooltip's IsOpen = true, the tooltip is shown, but binding has not been evaluated, unless I hover and wait first.
XAML:
<Window x:Class="TooltipApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TooltipApplication"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Source="http://www.brockmann-consult.de/beam/doc/help/visat/images/icons/Help22.png" MouseDown="Image_MouseDown" Stretch="None" MouseLeave="Image_MouseLeave">
<Image.ToolTip>
<ToolTip>
<TextBlock Text="{Binding Hint}"/>
</ToolTip>
</Image.ToolTip>
</Image>
</Grid>
</Window>
Code behind:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TooltipApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string Hint { get { return "This is my hint"; } }
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
var tooltip = (sender as FrameworkElement).ToolTip;
if (tooltip is ToolTip)
((ToolTip)tooltip).IsOpen = true;
}
private void Image_MouseLeave(object sender, MouseEventArgs e)
{
var tooltip = (sender as FrameworkElement).ToolTip;
if (tooltip is ToolTip)
((ToolTip)tooltip).IsOpen = false;
}
}
}
Using the code above, click the image before the tooltip timer elapses, or run it on a touch device.
Placing a breakpoint in the Hint getter also proves, that it is not called unless you wait for the tooltip timer.
How can I force the tooltip to evaluate its binding, and then show?
I'm trying to set the data context of a View to the list contained in it's ViewModel. But when I've tested the current set up, it seems the data context between the ViewModel and View is set incorrectly.
To debug this issue I set up a message box in the constructor of my View, and I get the following error message, which hints at the data context not being set correctly: "Object reference not set to an instance of an object"
The list is also used in another ViewModel which shows that the list is not empty, which hints further at a data context issue.
Does anyone know what the flaw is in setting up the data context between the View and ViewModel?
This is the ViewModel containing the list:
namespace LC_Points.ViewModel
{
public class ViewSubjectGradeViewModel
{
public ViewSubjectGradeViewModel()
{
AddedSubjectGradePairs = new ObservableCollection<ScoreModel>();
}
public ObservableCollection<ScoreModel> AddedSubjectGradePairs { get; set; }
}
}
And this is the View and View code behind:
<Page x:Class="LC_Points.View.ViewSubjectGradePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:LC_Points.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vms="using:LC_Points.ViewModel"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding ViewSubjectGradeViewModelProperty1}"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot">
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition />
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="20*" />
<RowDefinition Height="30*" />
<RowDefinition Height="30*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<!-- Title Panel -->
<StackPanel Grid.Row="0" Margin="19,0,0,0">
<TextBlock Margin="0,12,0,0"
Style="{ThemeResource TitleTextBlockStyle}"
Text="LC POINTS" />
<TextBlock Margin="0,-6.5,0,26.5"
CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"
Foreground="DarkGreen"
Style="{ThemeResource HeaderTextBlockStyle}"
Text="View Grades" />
</StackPanel>
<!-- TODO: Content should be placed within the following grid -->
<Grid x:Name="ContentRoot"
Grid.Row="1"
Margin="19,9.5,19,0">
<ListBox Height="400"
Margin="0,0,0,-329"
VerticalAlignment="Top"
ItemsSource="{Binding AddedSubjectGradePairs}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Subject}" /><Run Text=" - " /><Run Text="{Binding Points}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Page>
View code behind:
namespace LC_Points.View
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ViewSubjectGradePage : Page
{
private NavigationHelper navigationHelper;
private ObservableDictionary defaultViewModel = new ObservableDictionary();
public ViewSubjectGradePage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
var messageDialog = new MessageDialog(DataContext.GetType().ToString());
messageDialog.ShowAsync();
}
/// <summary>
/// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
/// </summary>
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
/// <summary>
/// Gets the view model for this <see cref="Page"/>.
/// This can be changed to a strongly typed view model.
/// </summary>
public ObservableDictionary DefaultViewModel
{
get { return this.defaultViewModel; }
}
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
}
private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
}
#region NavigationHelper registration
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedFrom(e);
}
#endregion
}
}
You may want to delete any generated code from your View's code behind that the template provides that you aren't using. This can cause confusion because the template wants you to use a local ObservableCollection as your DataContext.
There are 3 main ways of setting up a ViewModel to the DataContext of a View.
Use the code behind of the View:
public ViewSubjectGradePage()
{
this.InitializeComponent();
this.DataContext = new ViewSubjectGradeViewModel();
}
Use XAML (other Page properties removed for easier reading):
<Page x:Class="LC_Points.View.ViewSubjectGradePage"
xmlns:vms="using:LC_Points.ViewModel">
<Page.DataContext>
<vms:ViewSubjectGradeViewModel/>
</Page.DataContext>
</Page>
Use an MVVM framework like Prism. This will auto-wire your View and ViewModel based on standard naming conventions.
I prefer option 3 since it can provide a much more loosely coupled system, but may be overkill for a very small project plus there is a learning curve with any framework, but the benefits are great. Option 2 is my second vote since it keeps the code behind cleaner. Option 1 is something I don't do anymore, but is a nice quick way to get it working.
I believe this to be due to how you are assigning the DataContext. In your XAML, you are binding the DataContext to a property. I don't see anywhere in your code where you are actually assigning the data context to the defaultViewModel or the properties within it.
Try to update your constructor to the following.
public ViewSubjectGradePage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
this.DataContext = this.DefaultViewModel;
}
If you are wanting to assign it to a property within the DefaultViewModel, you may do so there as well. Then remove the DataContext assignment in your XAML.
Ive been playing with wpf and 2 way data binding to better understand it and ive noticed that when a textbox has 2 way data binding to a property the property is called twice. I have verified this by writing a value to the output window when the property is called. My code is below:-
My xaml
<Page
x:Class="_2waybindTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:_2waybindTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBox HorizontalAlignment="Left" Margin="55,93,0,0" TextWrapping="Wrap" Text="{Binding TestProperty, Mode=TwoWay}" VerticalAlignment="Top" Width="540"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="55,31,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
<TextBox HorizontalAlignment="Left" Margin="55,154,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="540"/>
</Grid>
</Page>
my simple viewmodel class to test
public class viewmodel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _TestProperty;
public void SetTestProperty()
{
this.TestProperty = "Set Test Property";
}
public string TestProperty{
get
{
return this._TestProperty;
}
set
{
this._TestProperty = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
}
Debug.WriteLine("this._TestProperty = " + this._TestProperty);
}
}
}
my xaml code behind
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DataContext = new viewmodel();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var vm = (viewmodel)DataContext;
vm.SetTestProperty();
}
}
Why is it called twice. Is this expected behaviour?
Generally, you should check if value actually changed, before firing a propertyChanged event, otherwise you may get into infinte cycle of binding updates. In your case, textbox is probably checking for change, preventing such cycle.
public string TestProperty{
set
{
if(this._TestProperty == value)
{
return;
}
this._TestProperty = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
}
}
}