user3265040
user3265040

Reputation: 315

Custom message popup WPF

This doesn't actually describe what I mean, but I'll try to explain. I've been using C# for an year now and never touched WPF. Only recently I've realized how awesome it is and started using it. I'm now facing a problem.

I want to let the user know the password/username are incorrect, so instead of the old WinForms MessageBox I want to make it more pleasent. I thought about creating a grid that tints the application darker, and then I'll be able to show some text on it. However - how's that possible?... Do you have any nicer ideas to show a message for the application (not a popup)? Thanks.

Upvotes: 1

Views: 3219

Answers (2)

Felipe
Felipe

Reputation: 266

You can create an UserControl with translucent background (like #33000000), and 3-row grid to show title, message and OK button, like bellow:

<UserControl x:Class="ApplicationNameSpace.MessageControl" ... >
    <Grid Background="#33000000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFFFFF" MinHeight="100" MinWidth="200">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="#EEEEEE">
                <Label Content="Unable to Login" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Grid>
            <Grid Grid.Row="1" Margin="10,20">
                <TextBlock Text="Wrong username or password. Try again." HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </Grid>
            <Grid Grid.Row="2" Background="#EEEEEE">
                <Button Content="OK" Width="80" Height="25" />
            </Grid>
        </Grid>
    </Grid>
</UserControl>

To use, you can add the control at the end of your window, and change the visibility to Visible when needs to show it.

<Window x:Class="ApplicationNameSpace.MainWindow"
        xmlns:local="clr-namespace:ApplicationNameSpace" ... >
    <Grid>
        ...
        <local:MessageControl Name="messageControl" Visibility="Collapsed" />
    </Grid>
</Window>

You can also create a generic control that you pass to a show method the title and message content, like MessageBox show method. You can also add the user control element programmatically in the window in this method.

MessageControl

Upvotes: 3

Overmachine
Overmachine

Reputation: 1733

You can use the validation and the INotifyDataError which is on WPF 4.5 and you can show a nice message next to the textbox check this link for example

Upvotes: 1

Related Questions