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");
}
}
}
Related
ok Im not sure if this issue is related to the library or me being still being new to WPF .but , I'm using the dragablz library and I am trying to re-add a tab after tearing it then closing that specfic window. However , I cant seem to find all the TabItem when ever I try to search for it in the grid of the current winodw I get nothing , this what I treid.
<Window x:Class="TeheMan8_Editor.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:TeheMan8_Editor"
xmlns:dragablz="http://dragablz.net/winfx/xaml/dragablz"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<dragablz:TabablzControl Name="tabHub" AllowDrop="True" Grid.ColumnSpan="2">
<dragablz:TabablzControl.InterTabController>
<dragablz:InterTabController />
</dragablz:TabablzControl.InterTabController>
<TabItem Name="mainTab" Header="Tab No. 1" IsSelected="True">
<Button Click="Button_Click">Open FileButton</Button>
</TabItem>
</dragablz:TabablzControl>
</Grid>
</Window>
CSharp Code:
using DiscUtils.Iso9660;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TeheMan8_Editor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var items = this.grid.Children.OfType<TabItem>(); //returns Empty
}
}
}
never mind I figured it out , the TabablzControl Object has a OfType function for the "Items.SouceCollection" property
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>
Hi i am making a WPF application.
I have a mainwindow, which i have my navigation in, then i also have a stackpanel, with this:
<Frame x:Name="_mainFrame" NavigationUIVisibility="Hidden"/>
inside, where i place my pages inside.
In My mainwindow i navigate to other pages using the following to for example move to the gameWindow:
private void NavigateGameWindow(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(new GameWindow());
}
This works fine, but now that i am inside that window (gameWindow), i am checking if a "player" is set, if not, i want to navigate to another page, where i can set certain values.
and then navigate back to GameWindow.
But how do i get a hold of _mainFrame, when it is a part of the mainwindow ?
It says in GameWindow on _mainFrame
The name _mainFrame, does not exist in the current context
Game Window
public partial class GameWindow
{
private int numberOfPlayers;
private Player[] players;
private INavigator _navigator;
public GameWindow(INavigator navigator)
{
_navigator = navigator; //assign navigator so i can navigate _mainframe to other pages.
// initialize game properties, check if they are set.
var gameProp = new GameProperties();
this.numberOfPlayers = 2;
this.players = gameProp.CheckPlayerIsSet(this.players);
//check if a player has been set
if (this.players != null)
{ // Player is set or has been set. proceed or start the game.
InitializeComponent();
}
else
{ // redirect to settings window because players has not been set!
_navigator.Navigate(new GameSettings());
}
}
}
Main Window
public partial class MainWindow : Window, INavigator
{
public MainWindow()
{
InitializeComponent();
}
private void ExitGame(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
public void Navigate(Page p)
{
_mainFrame.Navigate(p);
}
private void NavigateRulesWindow(object sender, RoutedEventArgs e)
{
Navigate(new GameRulesWindow());
}
private void NavigateGameWindow(object sender, RoutedEventArgs e)
{
Navigate(new GameWindow(this));
}
}
GameSettings
public partial class GameSettings : Page
{
public GameSettings()
{
InitializeComponent();
//var gameProps = new GameProperties();
// set number of players,, should prompt user, and get value!
//gameProps.SetNumberOfPlayers(2);
}
}
View for gamesettings
<Page x:Class="ITD.OOP_Projekt.WPF.Menu.GameSettings"
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:ITD.OOP_Projekt.WPF.Menu"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="GameSettings">
<Grid Background="white">
<TextBox HorizontalAlignment="Left" Height="23" Margin="229,144,0,0" TextWrapping="Wrap" Text="This is game settings" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
One very easy solution is this:
So with the following code you have only one Window (Mainwindow) and inside that Window you display your pages. You can compare it with your internet browser. You have one window and inside that window you can navigate between pages (settings, game, highscore, ...).
I hope this helps and you can get it to work!
If not i can try to upload a simple example to github in the evening.
Just get rid of your GameWindow and implement it as a page.
MainWindow
xaml:
<Window x:Class="PageSwitcher.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:PageSwitcher"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
</Grid>
cs:
public partial class MainWindow : INavigator
{
public MainWindow()
{
InitializeComponent();
Navigate( new Page1(this) );
}
public void Navigate( Page p )
{
MainFrame.Navigate( p );
}
}
Page1
xaml:
<Page x:Class="PageSwitcher.Page1"
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:PageSwitcher"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid Background="Green">
<Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page2" />
</Grid>
cs:
public partial class Page1 : Page
{
private INavigator _navigator;
public Page1(INavigator navigator)
{
_navigator = navigator;
InitializeComponent();
}
private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
{
_navigator.Navigate(new Page2(_navigator));
}
}
Page2
xaml:
<Page x:Class="PageSwitcher.Page2"
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:PageSwitcher"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2">
<Grid Background="Blue">
<Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page1"/>
</Grid>
cs:
public partial class Page2 : Page
{
private INavigator _navigator;
public Page2(INavigator navigator)
{
_navigator = navigator;
InitializeComponent();
}
private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
{
_navigator.Navigate(new Page1(_navigator ));
}
}
Thats all you really need.
In this example you can switch between two pages on button click events.
Just start a new wpf project and copy the code.
Play around with it until you understand it and then try to implement it in your game :)
If you want to redirect the page from one page to another you can create one method or function in which you can write the conditions and use the below code for redirection. It will take care of the directory also.
NavigationService.Navigate(new Settings.LatestActiveUsers());
I am very new to C# and the WPF Architecture. I am trying to create an application with simple page Navigation. I have changed my MainWindow from Window to NavigationWindow. I have also put the following code in MainWindow.xaml.cs
public void View(string newView)
{
Uri view = new Uri(newView, UriKind.Relative);
NavigationService nav = NavigationService.GetNavigationService(this);
if (nav != null)
{
nav.Navigate(view);
}
else
{
MessageBox.Show("NavNull");
}
}
I am calling this method from the default source ("HubView.xaml") this is in a folder called "Pages". The code looks like this
public void Button_Click(object sender, RoutedEventArgs e)
{
View = #"\Pages\UserAdd.xaml";
MainWindow mainWindow = new MainWindow();
mainWindow.View(View);
}
However when I click the button the message box is displayed indicating that nav equals null. I have read through this question and the subsequent answer, however I still do not fully understand.
Any suggestions would be greatly appreciated!
Edit:
This is the "MainWindow.xaml":
<NavigationWindow x:Class="Container.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="CanMinimize"
Title="MainWindow" Height="350" Width="525" Source="Pages/HubView.xaml" WindowStyle="ThreeDBorderWindow" ShowsNavigationUI="False">
</NavigationWindow>
As you can see the Window is a Navigation Window.
NavigationService is for page navigation in WPF like in a browser and for that to work you would need to add a Frame to your MainWindow.xaml
<Grid>
<Frame x:Name="mainFrame"/>
<Button x:Name="btnPage1" Content="Page 1" Width="200" Height="50"
Click="btnPage1_Click"/>
</Grid>
and use NavigationService to navigate eg
private void btnPage1_Click(object sender, RoutedEventArgs e)
{
mainFrame.NavigationService.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}
EDIT
Create 1 window and 2 pages
MainWindow.xaml
<NavigationWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Source="Page1.xaml">
Page1.xaml
<Page x:Class="WpfApplication1.Page1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<TextBlock Text="Page 1" FontWeight="Bold" />
<Button Margin="0,40" Content="Goto Page 2" Width="200" Height="50"
Click="Button_Click"/>
</Grid>
Page1.xaml.cs
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
}
}
NavigationWindow has a property of NavigationService so no need to create new variables. Hope that helps.
NOTE Page2.xaml is just a blank page
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