Click here to Skip to main content
15,895,011 members
Articles / Desktop Programming / WPF

WPFDialogs

Rate me:
Please Sign up or sign in to vote.
4.85/5 (20 votes)
24 Nov 2010CPOL12 min read 80.1K   1.4K   29  
How to Inherit From Custom Window and Create a dialog that returns a value using WPF and MVVM
<src:_ViewBase x:Class="MVVMDialogWithReturnProperty.MyDialogView"
               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:src="clr-namespace:MVVMDialogWithReturnProperty" 
               mc:Ignorable="d" 
               d:DesignHeight="252" 
               d:DesignWidth="454">
    
    <UserControl.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="75"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="Margin" Value="2"/>
            <Setter Property="FontFamily" Value="Verdana"/>
            <Setter Property="FontSize" Value="11px"/>
        </Style>
    </UserControl.Resources>

    <Grid Margin="5">

        <DockPanel LastChildFill="True">

            <!--Lower Button Panel-->
            <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Right"
                    DockPanel.Dock="Bottom">

                <!--Save Button-->
                <Button Name="cmdSave"
                        Command="{Binding SaveCommand}"
                        Content="Save">
                </Button>

                <!--Cancel Button-->
                <Button Name="cmdCancel"
                        Command="{Binding CancelCommand}"
                        Content="Cancel">
                </Button>

            </StackPanel>

            <Grid>

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>

                <Grid Margin="5,5,5,5" Grid.Row="1">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Label Name="lblTitle"
                            Content="Title:"
                            FontFamily="Arial"
                            Grid.Column="0" 
                            Grid.Row="0" 
                            HorizontalAlignment="Right"
                            FontSize="14"/>
                    <TextBox Name="txtTitle" 
                            Margin="5,2,5,2"
                            Text="{Binding Path=Title, Mode=TwoWay}"
                            Grid.Column="2" 
                            Grid.Row="0" 
                            VerticalAlignment="Top"
                            FontSize="12"/>

                    <!-- User Name-->
                    <Label Name="lblAge"
                            Content="Age:"
                            FontFamily="Arial"
                            Grid.Column="0" 
                            Grid.Row="1" 
                            HorizontalAlignment="Right"
                            FontSize="14"/>
                    <TextBox Name="txtAge" 
                            Margin="5,2,5,2"
                            Text="{Binding Path=Age, Mode=TwoWay}"
                            Grid.Column="1" 
                            Grid.Row="1"
                            VerticalAlignment="Top"
                            FontSize="12"/>
                    
                </Grid>

            </Grid>

        </DockPanel>

    </Grid>
    
</src:_ViewBase>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Marois Consulting
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions