How do i navigate from one Xaml file to another? - c#

I am very new to XAML and WPF.I have a problem.
I have two files. first.xaml and second.Xaml. There is a button in first.xaml, on click of which it should navigate to second.xaml.How do i achieve this.

This is one way of organising the code:
Your second.xaml should contain your window definiton e.g.:
<Window x:Class="MediaCheckerWPF.AboutBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About Media Checker" Height="300" Width="400" ResizeMode="NoResize"
Icon="/MediaCheckerWPF;component/Resources/checker.ico"
ShowInTaskbar="False">
<Grid>
...
</Grid>
</Window>
Your first.xaml has the button e.g.:
<Button Height="23" HorizontalAlignment="Right" Name="aboutButton"
VerticalAlignment="Top" Width="23" Click="AboutButton_Click"
Content="{DynamicResource TInformationButton}"
ToolTip="{DynamicResource TInformationButtonTooltip}" Margin="0,0,8,0"/>
Then in the code behind:
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
var about = new AboutBox { Owner = this };
about.Initialise();
about.Show();
}

ChrisF's answer is a good way of popping up a new window in a Windows-style application, except you should not call Initialize that way (it should be called from the constructor).
If you want web-style navigation instead, you should use the Page class instead of Window, along with NavigationService. This also allows your WPF application to run inside an actual web browser.

Related

UIElement.ManipulationDelta only Triggers Once on C# WPF

I want to implement an UI for finger controling, in WPF C# .Net framework, VS2019.
ManipulationDelta event is added on a Rectangle and it supposed to work continiously. But when I test it, it triggered only once at the moment I tapped(touch down). This event should be triggered continuously while my finger pressing.
Target: Show the position of pointer within the rectangle area.
XAML:
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,0,-322,-112">
<Rectangle
ManipulationDelta="Rectangle_ManipulationDelta"
TouchMove="Rectangle_TouchMove"
Fill="#FFF4F4F5"
HorizontalAlignment="Left"
Height="274" Margin="162,70,0,0"
Stroke="Black"
VerticalAlignment="Top"
Width="582" IsManipulationEnabled="True"/>
<TextBlock
x:Name="textBlock"
HorizontalAlignment="Left"
TextWrapping="Wrap"
VerticalAlignment="Top"
Margin="31,26,0,0"><Run Text="TextBlock"/></TextBlock>
</Grid>
Here is the event to print the postion of finger:
C#:
namespace WpfApp5{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Rectangle_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
}
private void Rectangle_TouchMove(object sender, TouchEventArgs e)
{
textBlock.Text = "Manipulation Touch: " + Mouse.GetPosition(Application.Current.MainWindow).ToString();
}
}
}
UI tested on Windows10 Simulator v16 and Surface Pro
Holding right-clicking is disabled
A test video is here to describe the issue with subtitle
Test Video
After testing, the event likes TouchMove, MouseMove, ManipulationDelta will be fired only if the cursor "get into" the border of element. I want to trigger this event within target area. Or should I use another event to achieve this?
Any suggestion is appreciated! Thank you.
I found that I use worng event, I should use TouchMove instead of ManipulationDelta(manipulation means manipulation for translation, zoom, etc, it's not necessary in this case).
And the flag should be set to false:
IsManipulationEnabled="false"
It working now! And thanks for who paticipating this discussion.

How to make a simple ContentDialog in .xaml?

What would it take to create a ContentDialog?
I basically have a button. When I tap that button the ContentDialog opens. The ContentDialog shall include a text like "this is an example" and 2 Buttons. How does the <ConTentDialog></ContentDialog> have to look like? Example:
<Page
x:Class="PDFViewerSDK_Win10.PDFReaderPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PDFViewerSDK_Win10"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" SizeChanged="OnSizeChanged">
<ContentDialog x:Name="test" PrimaryButtonText="Ok" SecondaryButtonText="Cancel" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" >
<Image Source="Assets/images/icon_menu.png"/>
</ContentDialog>
</Page>
A content dialog can be easily created in your code-behind. You can then have that C# code run on the click event for the button you mentioned you wanted.
Your other option is to create a content dialog on XAML, as you already posted in your original question, so that when the button is clicked, you just refer to the content dialog on your code-behind and call the ShowAsync() method.
I could write example code in this response, but in the interest of this question being relevant to visitors in the future I encourage you to refer to Microsoft's documentation which will be better maintained. I already read it and it does have specific examples that will be of great help to you.
Edit:
Posting sample code as a reference.
MainPage.XAML:
<Grid>
<Button
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="100" Width="250"
Content="Show Content"
FontSize="30px"
x:Name="ContentBtn"
Click="ContentBtn_Click"/>
<ContentDialog x:Name="ContentDialog"
Title="This is an example"
PrimaryButtonText="Ok"
CloseButtonText="Cancel"
DefaultButton="Primary">
</ContentDialog>
</Grid>
MainPage.XAML.cs:
private async void ContentBtn_Click(object sender, RoutedEventArgs e) {
await ContentDialog.ShowAsync();
}

Wpf - popup issue inside Usercontrol when click on button

I have a Window page, after click a button from window page --> then a UserControl page showing. After Inside UserControl there is a <Popup Name="MyPopup"> popup. The Popup always stays on top problem. How can I solve this issue ?
I have tried,
<Window 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" Background="Green">
<Grid >
<Button Height="50" Width="100" Content="window_ClickMe" Click="btnUserManage_Click"></Button>
<ContentControl Name="cont2" Visibility="Hidden">
</ContentControl>
</Grid>
</Window>
and code behind page of window,
private void btnUserManage_Click(object sender, RoutedEventArgs e)
{
UC_UserMgmt mw = new UC_UserMgmt();
cont2.Content = mw;
cont2.Visibility = Visibility.Visible;
}
then, this is usercontrol page with popup,
<UserControl x:Class="WpfApplication1.UC_UserMgmt"
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="400" Background="Blue">
<Grid>
<Grid Name="g1">
<Button Content="usercontrol_ClickMe" Height="50" Width="150" Margin="150,0,0,250" Click="btnShow_Click"></Button>
</Grid>
<Popup Name="MyPopup" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
HorizontalOffset="-150" Placement="Mouse" StaysOpen="{Binding ElementName=g1,Path=IsMouseOver}"
VerticalOffset="20"
AllowsTransparency="True">
<StackPanel>
<Border BorderBrush="Black" Background="Brown" BorderThickness="1" Width="300" Height="100" >
<Grid>
<TextBox x:Name="txtUName" HorizontalAlignment="Center" Height="28" Width="223" TextWrapping="Wrap" VerticalAlignment="Top" Margin="10,26,64.6,0" />
<Button Content="Open" Height="30" Width="50" Margin="238,24,9.6,43.6" Click="btnOpen_Click"/>
</Grid>
</Border>
</StackPanel>
</Popup>
</Grid>
</UserControl>
and this is code behind page of usercontrol,
private void btnOpen_Click(object sender, EventArgs e)
{
MyPopup.IsOpen = true;
System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
fDialog.Title = "Select file to be zip";
if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtUName.Text = fDialog.FileName.ToString();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
MyPopup.IsOpen = true;
}
The problem is, when user click on OPEN button, an openFileDialog is opening and when it is opened, the popup seems disappear. How can I solve this problem?
I believe using Popup is not the right choice in your situation. Take a look at Popup.StaysOpen Property, in particular the part that says
When the StaysOpen property is set to true, Popup stays open until it is explicitly closed by setting the IsOpen property to false. When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control.
I can't think of a clean way of keeping that popup open when showing the dialog box, because the dialog box is going to create a mouse/keyboard event when you interact with it
But to answer your question, I can think of two choices
1) You can simply move MyPopup.IsOpen = true; in btnOpen_Click to the end of the code block. I assume this will cause the popup to reopen after the dialog box is closed
2) You can create a boolean property or dependency property in your code behind set it to true when showing the popup and bind your Popup's StaysOpen to it, and maybe set it to false in LostFocus for g1 or something like that. Or even set StaysOpen to true and use IsOpen to close your Popup in g1's LostFocus
Edit - Second Solution How-To Don't use it, dirty code, bad practices, and the popup remains on top of dialogbox
In the Popup set StaysOpen="True"
In every control in the UserControl except the UserControl itself and the textbox set Focusable="False"
Add private bool dont; to the UserControl's code-behind and set it to True in the beginning of btnOpen_Click and set it to False in its end
In the UserControl and the TextBox add LostFocus="UserControl_LostFocus"
And then add this function in UserControl code-behind
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
if(!dont && !txtUName.IsFocused && !IsFocused)
MyPopup.IsOpen = false;
}

WPF UserControl loading dynamically based on which button is clicked

I am very new to WPF and this is my 1st application.
I have a Ribbon with different buttons. I want to load a UserControl based on which button is clicked.
I have a button called "Change Password" and I created the UserControl that will represent the UI to change the password.
I have another button called "Unlock Account" and I have a UserControl that can unlock an account.
App --> Ribbon --> RibbonButton --> SwappableUserControlAtRunTime
I want to use the same space in my Window to load/unload UserControls based on whats clicked.
I am using WPF 4.5 and .Net 4.5 on Windows 8.1
I am targeting PC's with .Net 4.5 and Windows 7+
You can load the UserControls in the ContentControl. Refer below code.
<RibbonWindow x:Class="LayoutWPF_Learning.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Ribbon Grid.Row="0">
<RibbonTab Header="Load UC">
<StackPanel Orientation="Horizontal">
<Button Content="UserControl1" Click="Button_Click"/>
<Button Content="UserControl2" Click="Button_Click_1"/>
</StackPanel>
</RibbonTab>
</Ribbon>
<ContentControl Grid.Row="1" x:Name="cntCtrl" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
cntCtrl.Content = new UserControl1();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
cntCtrl.Content = new UserControl2();
}
}
Even though you're going with codebehind (I'd recommend you look into MVVM) you'll be able to use a Converter to do this, this is more the WPF way of working than altering controls at runtime from your C# code.
There'll be loads of examples of how to use converters; Google "WPF Converter". Here's one that's relevant to your requirement: http://www.codeproject.com/Tips/285358/All-purpose-Boolean-to-Visibility-Converter

Launch WPF Class Library Project Full Screen

I have a WPF class library (with a user control within it being my launch page (similar to MainWindow).
Code Snippet for User control is as follows:
<UserControl x:Class="ABC" ...... >
<Grid>
<Label>Hello World</Label>
</Grid>
</UserControl>
I created another WPF app (labelled Launcher) which would essentially call the this class library.
That project doesnt have any MainWindow (removed it) and am launching the User Control from App.xaml
<Application
x:Class="Launcher.App"
......
StartupUri="pack://application:,,,/Project;component/View/HelloWorld.xaml">
The app doesnt start in full screen ever and always need to maxime it on launching.
I know the answer should be in lines of removing the StartupUri from App.xaml and putting the App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
//What to put here
}
Since there is no MainWindow hence there is I cant do this:
MainWindow wnd = new MainWindow();
wnd.Title = "Something else";
//Set StartupMode = "Maximized"
wnd.Show();
Do you know the Size of your Screen , if so
WindowStyle="None"
WindowStartupLocation="Manual"
Top="0" Left="0"
ResizeMode="NoResize"
BorderThickness="0"
Height="1080" Width="1920"
If you need to figure it out in code behind
WindowStyle="None"
WindowStartupLocation="Manual"
Top="0" Left="0"
ResizeMode="NoResize"
BorderThickness="0"
Height="{Binding Height}" Width="{Binding Width}"
In order to get the size look here :
The First Answer

Categories