Hi,@ Len G. Welcome to Microsoft Q&A. To create a display form and an interactive form in C# WPF (Windows Presentation Foundation), you'll typically use XAML for designing the user interface and C# for handling the logic and interaction. Here are examples of both a display form and an interactive form in WPF. Display Form: A display form is used to show information without allowing users to edit it. It typically contains labels, text blocks, or other controls to display data. Window1(DisplayWindow):
<Grid>
<Label Content="Name:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10"/>
<TextBlock Text="{Binding UserName}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="70,0,0,0"/>
</Grid>
public partial class Window1 : Window
{
public Window1(string userName)
{
InitializeComponent();
DataContext = new DisplayViewModel(userName);
}
}
public class DisplayViewModel
{
public string UserName { get; }
public DisplayViewModel(string userName)
{
UserName = userName;
}
}
Interactive Form: An interactive form allows users to input or edit data. It typically contains text boxes, combo boxes, buttons, etc., to facilitate user interaction. MainWindow:
<Grid>
<Label Content="Name:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10"/>
<TextBox Text="{Binding UserName}" VerticalAlignment="Center" HorizontalAlignment="Left" Width="100" Margin="70,0,0,0"/>
<Button Content="Submit" Click="SubmitButton_Click" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,30,0,0"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new InteractiveViewModel(); // Assign view model to provide data and handle interaction
}
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
string userName = ((InteractiveViewModel)DataContext).UserName;
Window1 displayWindow = new Window1(userName);
displayWindow.Show();
}
}
public class InteractiveViewModel : INotifyPropertyChanged
{
private string _userName;
public string UserName
{
get => _userName;
set
{
_userName = value;
OnPropertyChanged(nameof(UserName));
}
}
// Add more properties for other data fields
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.