WPF button gif animation - wpf

I'm trying to achieve one Login button which changes to a Please wait... mode once pressed. I was thinking about some StoryBoard animation inside button or GIF image animation. Seems I'm pretty new to WPF and after several search attempts, I'm unable to find a fully working idea.
Login button idea
What would be the best way to do the above thing? Is there any way to eliminate the GIF image and create the same using some Path or something similar? Also note that I want this behavior of button to be triggered on Button.Click event.

Just simply add WpfAnimatedGif from your visual studio Tools -> NuGet Package Manager->Package Manager Console. There you find the Package adder window and type pm > Install-Package WpfAnimatedGif. After a few seconds package should be added. Now firstly, you have add namespace xmlns:gif="http://wpfanimatedgif.codeplex.com" then design your Button.
<Window x:Class="WpfApplication1.MainWindow" Name="root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
Width="500" Height="500" DataContext="{Binding ElementName=root}">
<StackPanel>
<Button Name="BtnLogin" Width="100" Height="30" Content="Login" Background="Red" Margin="0,0,0,5" Click="Button_Click"/>
<Button Width="100" Height="30" Background="Red">
<Grid>
<Image gif:ImageBehavior.AnimatedSource="{Binding DataContext.LoadingImage}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<TextBlock Text="Please Wait" Padding="30,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Button>
</StackPanel>
</Window>
The following .cs file is below and when you need to stop loading image, just assign LoadingImage = null;
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}
private ImageSource _LoadingImage;
public ImageSource LoadingImage
{
get { return _LoadingImage; }
set
{
_LoadingImage = value;
OnPropertyChanged("LoadingImage");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LoadingImage = GetBitmapImage("/aaaa.gif");
}
public static BitmapImage GetBitmapImage(String location)
{
BitmapImage image = null;
try
{
Uri iconUri = new Uri("pack://application:,,,/" + ";component" + location, UriKind.RelativeOrAbsolute);
image = new BitmapImage(iconUri);
}
catch (Exception ex)
{
}
return image;
}
}

Related

c# wpf Dialog will close the owner window

i have a window that is acting like a Dialog with two buttons and a textbox, so its an input dialog, the problem is when i press the buttons, or close the dialog window in any way the main window that i used to create this dialog will get closed too! (exit code = 0) and another thing is that it will work OK in vs debugging but when i run the app without vs debugging that owner window get closed and in my case that window is the Main app window so the whole application will shutdown! what am i doing wrong?
XAML:
<Window x:Class="Server.Forms.Dialogs.PokeDialog"
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:Server.Forms.Dialogs"
mc:Ignorable="d"
Title="Poke" MinHeight="120" MinWidth="225"
ResizeMode="CanResizeWithGrip"
SizeToContent="WidthAndHeight"
x:Name="windowPoke"
Loaded="windowPoke_Loaded">
<StackPanel VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Margin="25">
<Label Content="Poke Reason:"
Padding="0"/>
<TextBox x:Name="textboxPokeReason"
Margin="0 5"/>
<DockPanel>
<Button x:Name="btnOk"
Click="btnOk_Click"
IsDefault="True"
Content="OK"
MinWidth="100"
Margin="0 0 5 0"/>
<Button x:Name="btnCancel"
Click="btnCancel_Click"
IsCancel="True"
Content="Cancel"
MinWidth="100"/>
</DockPanel>
</StackPanel>
C#:
public partial class PokeDialog : Window
{
public string PokeReason
{
get { return textboxPokeReason.Text; }
}
public PokeDialog()
{
InitializeComponent();
}
private void windowPoke_Loaded(object sender, RoutedEventArgs e)
{
textboxPokeReason.Focus();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
}
the way i used dialog:
private void btnPokeClient_Click(object sender, RoutedEventArgs e)
{
var dialog = new PokeDialog();
if (dialog.ShowDialog() == true)
{
MessageBox.Show("TRUE - input text:" + dialog.PokeReason);
}
else MessageBox.Show("FALSE");
}
i can see the messageboxes too, but after closing this message boxes the whole app will get closed

APPCRASH on wpfgfx_v0400.dll only on 32-bit systems

I have an issue with a WPF application that I'm writing. I have a window that I load with profile pictures for the user to choose from when setting up an account within the application. Each picture is loaded into a user control, and placed in a stackpanel, so that when the user clicks the picture, it triggers the code in the user control, and automatically sets their profile picture without any more clicking needed. This window loads on 64-bit systems just fine. However, when loading on a 32-bit system, the entire application crashes. The faulting module is wpfgfx_v0400.dll. I don't know why it's crashing. Please help.
Here's the error in the Event Viewer:
Here's the XAML on the frontend of the window in question:
<Window x:Class="RandomApplication.Windows.ChooseProfilePic"
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:RandomApplication.Windows"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
ContentRendered ="On_ContentRendered"
Title="Choose A Picture" Height="515" Width="500" Background="Black" ResizeMode="CanMinimize">
<Grid Background="Black">
<ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Height="420" VerticalAlignment="Top" Margin="0,5,0,0">
<StackPanel>
<StackPanel Name="HeadsPanel" Orientation="Horizontal" Height="100" VerticalAlignment="Top"/>
<StackPanel Name="AbstractPanel" Orientation="Horizontal" Height="100" VerticalAlignment="Top"/>
<StackPanel Name="ShapesPanel" Orientation="Horizontal" Height="100" VerticalAlignment="Top"/>
<StackPanel Name="MiscPanel" Orientation="Horizontal" Height="100" VerticalAlignment="Top"/>
</StackPanel>
</ScrollViewer>
<Button Name="CancelButton" VerticalAlignment="Bottom" Style="{DynamicResource RedButton}" Click="CancelButton_Click">Cancel</Button>
<Border Name="LoadingBorder" Background="Black">
<TextBlock Name="LoadingLabel" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,150" FontSize="20" FontWeight="Bold">
<Run>Loading Pictures</Run>
<LineBreak></LineBreak>
<Run>Please Wait...</Run>
</TextBlock>
</Border>
</Grid>
Here's the code behind the window:
namespace RandomApplication.Windows
{
public partial class ChooseProfilePic : Window
{
private readonly BackgroundWorker _loadPictureWorker = new BackgroundWorker();
public ChooseProfilePic()
{
InitializeComponent();
Topmost = true;
_loadPictureWorker.DoWork += LoadImages;
_loadPictureWorker.RunWorkerCompleted += LoadImages_Completed;
}
private void On_ContentRendered(object sender, EventArgs e)
{
_loadPictureWorker.RunWorkerAsync();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void LoadImages(object sender, DoWorkEventArgs e)
{
try
{
var headsImagePath = AppDomain.CurrentDomain.BaseDirectory + #"Images\Profile Pics\Heads\";
var abstractImagePath = AppDomain.CurrentDomain.BaseDirectory + #"Images\Profile Pics\Abstract\";
var shapesImagePath = AppDomain.CurrentDomain.BaseDirectory + #"Images\Profile Pics\Shapes\";
var miscImagePath = AppDomain.CurrentDomain.BaseDirectory + #"Images\Profile Pics\Misc\";
List<string> headsImageList = GetImages(headsImagePath);
List<string> abstractImageList = GetImages(abstractImagePath);
List<string> shapesImageList = GetImages(shapesImagePath);
List<string> miscImageList = GetImages(miscImagePath);
Application.Current.Dispatcher.Invoke(() =>
{
LoadViewingPanel(headsImageList, HeadsPanel);
LoadViewingPanel(abstractImageList, AbstractPanel);
LoadViewingPanel(shapesImageList, ShapesPanel);
LoadViewingPanel(miscImageList, MiscPanel);
});
}
catch (Exception ex)
{
CustomMessageBox.Show("Could not load images. :-(", "Image Retrieval Failed", MessageBoxButton.OK,
MessageBoxImage.Error);
Helper.WriteException(Helper.ErrorLogs + "Error Loading Images.txt", ex);
}
}
private void LoadImages_Completed(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
CustomMessageBox.Show("Could not load images. :-(", "Image Retrieval Failed", MessageBoxButton.OK,
MessageBoxImage.Error);
Helper.WriteException(Helper.ErrorLogs + "Error Loading Images.txt", e.Error);
}
else LoadingBorder.Visibility = Visibility.Hidden;
}
public List<string> GetImages(string imagePath)
{
var that = GetAllFiles(imagePath);
return that.ToList();
}
private void LoadViewingPanel(List<string> list, StackPanel panel)
{
foreach (var imageString in list)
{
Helper.WriteLineToFile(Helper.ErrorLogs + "2nd Info Loading Images.txt", imageString);
var thisUri = new Uri(imageString, UriKind.RelativeOrAbsolute);
var pic = new ProfilePic {ProfilePicImage = {Source = new BitmapImage(thisUri)}};
panel.Children.Add(pic);
}
}
private IEnumerable<string> GetAllFiles(string path)
{
return Directory.EnumerateFiles(path, "*.jpg").Union(
Directory.EnumerateDirectories(path).SelectMany(d =>
{
try
{
return GetAllFiles(d);
}
catch
{
return Enumerable.Empty<string>();
}
}));
}
}
}
I've researched what could cause issues with this particular dll, but none of it seems to relate to my issue.
So, I figured out the issue. Apparently, the size of the images that I was trying to load was too big. The images were all 2048x2048 pixels, which made them anywhere from 180 KB to 380 KB in size. Apparently this is too much. I resized all of the pictures to 100x100 pixels (as I was only ever presenting them to the user as 100x100), which brought the file sizes down to 7 - 10 KB each. After that, they loaded just fine with no crashing issues.

WPF List selection issue when opening files

Got a WPF application which has an on hover popup. The popup contains a list of different files which can be opened (e.g. pdf, excel etc)
You can navigate and select a file by double clicking and it opens as you would expect.
But if I now navigate to a different file I can see that the on hover selection isn't now working,
If you now select a different file, the original file is opened again.
I am using a Process.Start and passing the full path to the file to the method.
The application is a fair size so here are some excerpts for a Test application I have written to look into this further
The XAML for the main window
<Window x:Class="TestPopupIssue.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">
<Canvas Margin="5" Background="Red" Width="200" Height="150" >
<Rectangle Name="rectangle1"
Canvas.Top="60" Canvas.Left="50"
Height="85" Width="60"
Fill="Black" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave" />
<Popup x:Name="PopupWindow" PlacementTarget="{Binding ElementName=rectangle1}" Placement="Top" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave">
<ListBox MinHeight="50" ItemsSource="{Binding Files}" MouseDoubleClick="FileList_MouseDoubleClick"`enter code here` x:Name="FileList" />
</Popup>
</Canvas>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
FileList f;
public MainWindow()
{
InitializeComponent();
f = new FileList();
f.PopulateFiles();
this.DataContext = f;
}
private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FileList.SelectedItem != null)
{
string item = FileList.SelectedItem as string;
if (item != null)
{
System.Diagnostics.Process.Start(item);
}
}
}
private void rectangle1_MouseEnter(object sender, MouseEventArgs e)
{
PopupWindow.IsOpen = true;
}
private void rectangle1_MouseLeave(object sender, MouseEventArgs e)
{
PopupWindow.IsOpen = false;
}
}
And there is a FileList class which just has a generic string list of file paths called
Files
Thanks
I have tested your Sample-Application, when your opening the File with Process.Start your Focus gets stolen by the Application that opens your File.
Somehow the ListBox in the Popup canĀ“t change their SelectedItem when the Window has lost his Focus.
Unfortunately I have not managed to get the focus back on the Window, this.SetFocus() has not worked for me.
Anyway another possible Solution would be to close the Popup when your opening the File.
private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FileList.SelectedItem != null)
{
string item = FileList.SelectedItem as string;
if (item != null)
{
System.Diagnostics.Process.Start(item);
PopupWindow.IsOpen = false;
}
}
}
this way the ListBox can update the selectedItem again.
hope this helps!

Binding the "WindowState" property of a window in WPF using MVVM

I bound the "WindowState" property of my main window to my ViewModel in order to change the state of the window by a command, but the first time I minimize the window it minimizes like a worksheet does in an Excel file. Is there a work around for this or a correct way to bind the "WindowState" property to my ViewModel so that the window minimizes correctly?
this is a sample work around that tested with Relaying Command Logic. You will get more detail on WPF Apps With The Model-View-ViewModel Design Pattern .
<Window x:Class="WpfMvvmTestCSharp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfMvvmTestCSharp"
Title="Window1" Height="300" Width="300" WindowState="{Binding CurWindowState, Mode=TwoWay}">
<Window.DataContext>
<vm:Window1ViewModel/>
</Window.DataContext>
<Grid>
<Button Command="{Binding CmdMax}" Height="23" Margin="12,25,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Maximize</Button>
<Button Command="{Binding CmdMin}" Height="23" Margin="101,25,102,0" Name="button2" VerticalAlignment="Top">Minimize</Button>
<Button Command="{Binding CmdRes}" Height="23" HorizontalAlignment="Right" Margin="0,25,13,0" Name="button3" VerticalAlignment="Top" Width="75">Restore</Button>
</Grid>
</Window>
and in the Windows ViewModel
class Window1ViewModel:ViewModelBase
{
public Window1ViewModel()
{
CurWindowState = WindowState.Maximized;
}
public ICommand CmdMax
{
get { return new RelayCommand(param => onCmdMax()); }
}
void onCmdMax()
{
CurWindowState = WindowState.Maximized;
}
public ICommand CmdMin
{
get { return new RelayCommand(param => onCmdMin()); }
}
void onCmdMin()
{
CurWindowState = WindowState.Minimized;
}
public ICommand CmdRes
{
get { return new RelayCommand(param => onCmdRes()); }
}
void onCmdRes()
{
CurWindowState = WindowState.Normal;
}
private WindowState _curWindowState;
public WindowState CurWindowState
{
get
{
return _curWindowState;
}
set
{
_curWindowState = value;
base.OnPropertyChanged("CurWindowState");
}
}
}
I don't think you should care about the window state in a view model, it's completely wrong because a lower-level layer is aware of a higher-level layer (thus wrong Separation of Concerns (SOC)).
What I normally do in this case is subscribe to changes in the view model from the code-behind of the control or window (thus the view) containing the view model. In this case, it is valid to write code in the code-behind because it is only used in the view (and thus the code-behind is the perfect location for this logic, which you really don't want to unit test).
Another option to consider is subscribing both via a command AND an event to code behind, e.g:
<Button Command="{Binding SnoozeCommand}" Click="Button_Click">Snooze</Button>
The command in this case affects the VM. The Click event, only changes the Window state.
I have found my own solution which is perfectly suited to MVVM. I'm using behavior to find the parent window of the user control and track WindowState changes.
public class WindowStateBehavior : Behavior<UserControl>
{
public static readonly DependencyProperty WindowStateProperty =
DependencyProperty.Register(nameof(WindowState), typeof(WindowState), typeof(WindowStateBehavior),
new FrameworkPropertyMetadata(default(WindowState), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
private Window window;
public WindowState WindowState
{
get => (WindowState) GetValue(WindowStateProperty);
set => SetCurrentValue(WindowStateProperty, value);
}
protected override void OnAttached()
{
base.OnAttached();
this.window = Window.GetWindow(this.AssociatedObject)!;
this.window.StateChanged += this.OnStateChanged;
}
private void OnStateChanged(object sender, EventArgs e) => this.WindowState = this.window.WindowState;
}
This behavior can be used in any UserControl like this with bound WindowState in ViewModel.
<UserControl x:Class="RCBase.WPF.Monitor.CustomUserView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:behaviors="clr-namespace:RCBase.WPF.Behaviors"
mc:Ignorable="d" d:DataContext="{d:DesignInstance monitor:CustomUserViewModel}">
<i:Interaction.Behaviors>
<behaviors:WindowStateBehavior WindowState="{Binding WindowState}" />
</i:Interaction.Behaviors>

WPF: Create a dialog / prompt

I need to create a Dialog / Prompt including TextBox for user input. My problem is, how to get the text after having confirmed the dialog? Usually I would make a class for this which would save the text in a property. However I want do design the Dialog using XAML. So I would somehow have to extent the XAML Code to save the content of the TextBox in a property - but I guess that's not possible with pure XAML. What would be the best way to realize what I'd like to do? How to build a dialog which can be defined from XAML but can still somehow return the input? Thanks for any hint!
The "responsible" answer would be for me to suggest building a ViewModel for the dialog and use two-way databinding on the TextBox so that the ViewModel had some "ResponseText" property or what not. This is easy enough to do but probably overkill.
The pragmatic answer would be to just give your text box an x:Name so that it becomes a member and expose the text as a property in your code behind class like so:
<!-- Incredibly simplified XAML -->
<Window x:Class="MyDialog">
<StackPanel>
<TextBlock Text="Enter some text" />
<TextBox x:Name="ResponseTextBox" />
<Button Content="OK" Click="OKButton_Click" />
</StackPanel>
</Window>
Then in your code behind...
partial class MyDialog : Window {
public MyDialog() {
InitializeComponent();
}
public string ResponseText {
get { return ResponseTextBox.Text; }
set { ResponseTextBox.Text = value; }
}
private void OKButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
DialogResult = true;
}
}
Then to use it...
var dialog = new MyDialog();
if (dialog.ShowDialog() == true) {
MessageBox.Show("You said: " + dialog.ResponseText);
}
Edit: Can be installed with nuget https://www.nuget.org/packages/PromptDialog/
I just add a static method to call it like a MessageBox:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Class="utils.PromptDialog"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
SizeToContent="WidthAndHeight"
MinWidth="300"
MinHeight="100"
WindowStyle="SingleBorderWindow"
ResizeMode="CanMinimize">
<StackPanel Margin="5">
<TextBlock Name="txtQuestion" Margin="5"/>
<TextBox Name="txtResponse" Margin="5"/>
<PasswordBox Name="txtPasswordResponse" />
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right">
<Button Content="_Ok" IsDefault="True" Margin="5" Name="btnOk" Click="btnOk_Click" />
<Button Content="_Cancel" IsCancel="True" Margin="5" Name="btnCancel" Click="btnCancel_Click" />
</StackPanel>
</StackPanel>
</Window>
And the code behind:
public partial class PromptDialog : Window
{
public enum InputType
{
Text,
Password
}
private InputType _inputType = InputType.Text;
public PromptDialog(string question, string title, string defaultValue = "", InputType inputType = InputType.Text)
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(PromptDialog_Loaded);
txtQuestion.Text = question;
Title = title;
txtResponse.Text = defaultValue;
_inputType = inputType;
if (_inputType == InputType.Password)
txtResponse.Visibility = Visibility.Collapsed;
else
txtPasswordResponse.Visibility = Visibility.Collapsed;
}
void PromptDialog_Loaded(object sender, RoutedEventArgs e)
{
if (_inputType == InputType.Password)
txtPasswordResponse.Focus();
else
txtResponse.Focus();
}
public static string Prompt(string question, string title, string defaultValue = "", InputType inputType = InputType.Text)
{
PromptDialog inst = new PromptDialog(question, title, defaultValue, inputType);
inst.ShowDialog();
if (inst.DialogResult == true)
return inst.ResponseText;
return null;
}
public string ResponseText
{
get
{
if (_inputType == InputType.Password)
return txtPasswordResponse.Password;
else
return txtResponse.Text;
}
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
So you can call it like:
string repeatPassword = PromptDialog.Prompt("Repeat password", "Password confirm", inputType: PromptDialog.InputType.Password);
Great answer of Josh, all credit to him, I slightly modified it to this however:
MyDialog Xaml
<StackPanel Margin="5,5,5,5">
<TextBlock Name="TitleTextBox" Margin="0,0,0,10" />
<TextBox Name="InputTextBox" Padding="3,3,3,3" />
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="BtnOk" Content="OK" Grid.Column="0" Margin="0,0,5,0" Padding="8" Click="BtnOk_Click" />
<Button Name="BtnCancel" Content="Cancel" Grid.Column="1" Margin="5,0,0,0" Padding="8" Click="BtnCancel_Click" />
</Grid>
</StackPanel>
MyDialog Code Behind
public MyDialog()
{
InitializeComponent();
}
public MyDialog(string title,string input)
{
InitializeComponent();
TitleText = title;
InputText = input;
}
public string TitleText
{
get { return TitleTextBox.Text; }
set { TitleTextBox.Text = value; }
}
public string InputText
{
get { return InputTextBox.Text; }
set { InputTextBox.Text = value; }
}
public bool Canceled { get; set; }
private void BtnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
{
Canceled = true;
Close();
}
private void BtnOk_Click(object sender, System.Windows.RoutedEventArgs e)
{
Canceled = false;
Close();
}
And call it somewhere else
var dialog = new MyDialog("test", "hello");
dialog.Show();
dialog.Closing += (sender,e) =>
{
var d = sender as MyDialog;
if(!d.Canceled)
MessageBox.Show(d.InputText);
}
You don't need ANY of these other fancy answers. Below is a simplistic example that doesn't have all the Margin, Height, Width properties set in the XAML, but should be enough to show how to get this done at a basic level.
XAML
Build a Window page like you would normally and add your fields to it, say a Label and TextBox control inside a StackPanel:
<StackPanel Orientation="Horizontal">
<Label Name="lblUser" Content="User Name:" />
<TextBox Name="txtUser" />
</StackPanel>
Then create a standard Button for Submission ("OK" or "Submit") and a "Cancel" button if you like:
<StackPanel Orientation="Horizontal">
<Button Name="btnSubmit" Click="btnSubmit_Click" Content="Submit" />
<Button Name="btnCancel" Click="btnCancel_Click" Content="Cancel" />
</StackPanel>
Code-Behind
You'll add the Click event handler functions in the code-behind, but when you go there, first, declare a public variable where you will store your textbox value:
public static string strUserName = String.Empty;
Then, for the event handler functions (right-click the Click function on the button XAML, select "Go To Definition", it will create it for you), you need a check to see if your box is empty. You store it in your variable if it is not, and close your window:
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtUser.Text))
{
strUserName = txtUser.Text;
this.Close();
}
else
MessageBox.Show("Must provide a user name in the textbox.");
}
Calling It From Another Page
You're thinking, if I close my window with that this.Close() up there, my value is gone, right? NO!! I found this out from another site: http://www.dreamincode.net/forums/topic/359208-wpf-how-to-make-simple-popup-window-for-input/
They had a similar example to this (I cleaned it up a bit) of how to open your Window from another and retrieve the values:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenPopup_Click(object sender, RoutedEventArgs e)
{
MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page
//ShowDialog means you can't focus the parent window, only the popup
popup.ShowDialog(); //execution will block here in this method until the popup closes
string result = popup.strUserName;
UserNameTextBlock.Text = result; // should show what was input on the other page
}
}
Cancel Button
You're thinking, well what about that Cancel button, though? So we just add another public variable back in our pop-up window code-behind:
public static bool cancelled = false;
And let's include our btnCancel_Click event handler, and make one change to btnSubmit_Click:
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
cancelled = true;
strUserName = String.Empty;
this.Close();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtUser.Text))
{
strUserName = txtUser.Text;
cancelled = false; // <-- I add this in here, just in case
this.Close();
}
else
MessageBox.Show("Must provide a user name in the textbox.");
}
And then we just read that variable in our MainWindow btnOpenPopup_Click event:
private void btnOpenPopup_Click(object sender, RoutedEventArgs e)
{
MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page
//ShowDialog means you can't focus the parent window, only the popup
popup.ShowDialog(); //execution will block here in this method until the popup closes
// **Here we find out if we cancelled or not**
if (popup.cancelled == true)
return;
else
{
string result = popup.strUserName;
UserNameTextBlock.Text = result; // should show what was input on the other page
}
}
Long response, but I wanted to show how easy this is using public static variables. No DialogResult, no returning values, nothing. Just open the window, store your values with the button events in the pop-up window, then retrieve them afterwards in the main window function.

Resources