I want to develop a desktop application based on WPF. How do I navigate through C # code from one window to another in a window? in the other word, I have tried to set up a click event for a radiobutton that opens another window in frame or border.
<Window x:Class="WpfNavigation2.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:WpfNavigation2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid>
<Border Margin="10,100,10,10" Background="AliceBlue">
<Grid Name="contergrid">
</Grid>
</Border>
<RadioButton x:Name="radioButton" Content="RadioButton1" HorizontalAlignment="Left" Margin="60,0,0,0" VerticalAlignment="Top" Click="btn1"/>
<RadioButton x:Name="radioButton1" Content="RadioButton2" HorizontalAlignment="Left" Margin="60,37,0,0" VerticalAlignment="Top" Click="btn2"/>
<RadioButton x:Name="radioButton2" Content="RadioButton3" HorizontalAlignment="Left" Margin="60,68,0,0" VerticalAlignment="Top" Click="btn3"/>
</Grid>
</Window>
private void btn1(object sender, RoutedEventArgs e)
{
//open window 1 in border
}
What you are looking for is a ContentPresenter. You can put all kinds of content in a ContentPresenter for example a control
<Border Margin="10,100,10,10" Background="AliceBlue">
<ContentPresenter Name="contentFrame">
</ContentPresenter>
</Border>
private void btn1(object sender, RoutedEventArgs e)
{
Grid grd = new Grid();
grd.Background = new SolidColorBrush(Colors.HotPink);
contentFrame.Content = grd; // replace me with your control / page / window1
}
Related
1- Copy and paste the following code to the MainWindow.xaml file.
<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">
<StackPanel>
<Button x:Name="Button1" Height="25" Width="100" Content="Press Me" Click="Button1_Click"/>
<TextBox x:Name="TextBox1" Width="400" Height="200" TextWrapping="Wrap" TextAlignment="Justify" AcceptsReturn="True"/>
</StackPanel>
2- Copy and paste the following code to the code behind file.
private void Button1_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
myStringBuilder.Append("Orange").AppendLine();
myStringBuilder.Append("").AppendLine();
myStringBuilder.Append("Apple").AppendLine();
myStringBuilder.Append("Banana").AppendLine();
myStringBuilder.Append("").AppendLine();
myStringBuilder.Append("Plum").AppendLine();
TextBox1.Text = myStringBuilder.ToString();
}
3- Run this project and then press Button1.
My question:
How can I manage desired result via only XAML code?
(I dont want to use C# code)
The question:
Newline in string attribute
contains the answer to your question I believe.
<TextBox x:Name="TextBox1" Width="400" Height="200" TextWrapping="Wrap" TextAlignment="Justify" AcceptsReturn="True" Text="A
B" />
I am working on a WPF project. I need to display an image when the new window opens, but I don't seem to get it to work. I feel like I've tried everything.
<Window x:Class="iTool.ProfileWindow"
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:iTool"
mc:Ignorable="d"
Title="iTool: User Profile" Height="800" Width="1200" ResizeMode="NoResize">
<Grid>
<StackPanel>
<Label Content="User Profile" FontSize="30" HorizontalContentAlignment="Center"/>
<StackPanel Orientation="Horizontal">
<StackPanel Width="300" Height="700">
<Image x:Name="imgUserProfile" Height="200" Width="200" Margin="50"/>
<TextBlock x:Name="txbFirstName"/>
<TextBlock x:Name="txbLastName"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Window>
public partial class ProfileWindow : Window
{
public ProfileWindow()
{
InitializeComponent();
imgUserProfile.Source = new BitmapImage(new Uri(MainWindow.activeUserImage, UriKind.RelativeOrAbsolute));
}
}
In Debug watch:
activeUserImage = "images/samson_profile.jpg"
imgUserProfile.Source = {pack://application:,,,/iTool;component/images/samson_profile.jpg}
The image exists in the "images" folder, but is not added to the project as existing file.
When the ProfileWindow opens all I can see is User Profile label, no image.
I'm creating a little paint program and I want to have a canvas pop up when I click on a button.
private void buttonNewDrawing_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(mainCanvas);
}
Doesn't work obviously because it's asking for a string. but what can I then use instead of a MessageBox to just have the canvas mainCanvas pop up so I can use it?
Thank you in advance.
MainWindow.xaml:
<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">
<Grid>
<Button Click="Button_Click" Grid.Row="2" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" VerticalContentAlignment="Center" Content="Click"/>
</Grid>
</Window>
MainWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 window = new Window1();
window.Show();
}
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Canvas />
</Grid>
</Window>
I want to show whole of the ListBox when its bounds are overflowed from main window. DatePicker behaves just I want to do (see the attached image). How can I implement to do this?
Screen capture describing what DatePicker can do and I can't do
<!-- MainWindow.xaml -->
<Window x:Class="OverflowSample.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:OverflowSample"
mc:Ignorable="d"
Title="MainWindow" Height="130" Width="500">
<Grid>
<DatePicker HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Button x:Name="buttonShowListBox" Content="Show" HorizontalAlignment="Left" Margin="240,10,0,0" VerticalAlignment="Top" Width="75" Click="buttonShowListBox_Click"/>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="160" Margin="250,40,0,-90" VerticalAlignment="Top" Width="200" FontSize="24" Visibility="Collapsed">
<ListBoxItem Content="Three"/>
<ListBoxItem Content="Two"/>
<ListBoxItem Content="One"/>
<ListBoxItem Content="Zero"/>
</ListBox>
</Grid>
</Window>
// MainWindow.xaml.cs
using System.Windows;
namespace OverflowSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void buttonShowListBox_Click(object sender, RoutedEventArgs e)
{
listBox.Visibility = (listBox.Visibility == Visibility.Visible) ?
Visibility.Collapsed :
Visibility.Visible;
}
}
}
DatePicker uses a Popup and puts the calendar inside that.
the .net framework source is available to see for yourself:
http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/DatePicker.cs,c20427230c18ba13
I made following application (as a test)
XAML:
<Window x:Class="GUITest.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 Background="Transparent">
<TextBlock Text="openDialog" Background="Red" HorizontalAlignment="Center" VerticalAlignment="Top" MouseDown="TextBlock_MouseDown" />
</Grid>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
Console.Out.WriteLine(dlg.FileName);
}
}
}
I catch the mouseDown event because it catches both mouse button down events and touch down events. The code has the expected behavior for mouse clicks. Touch gives me some trouble.
If i touch the TextBlock it opens a dialog window as asked. after closing it, any touch on the window opens the dialog window, even if the touch was not on the TextBlock.
Is this a bug? Can i work around this?
EDIT: i posted a workaround, an actual fix would still be usefull
For other people who run into the same problem. This isn't a fix to the problem, but it is a workaround.
I used a button and restyled it to look like a TextBlock
XAML:
<Grid Background="Transparent">
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click" Content="openDialog">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
Code for Button_Click is the same as TextBlock_MouseDown