How to populate WPF window from database [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to auto load some data from database to populate some text boxes and combo boxes when a new window is loaded.
For example when I click a button in window1, window1 will show a new window window2 and pass an ID to window2 (id is required for querying database).
How can i do that?
Thanks,

This is just a very simple example of what you can do:
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 Name="btn1" Click="btn1_Click" Content="Button" Margin="10,10,361,283"></Button>
</Grid>
</Window>
MainWindow.xaml.cs
private void btn1_Click(object sender, RoutedEventArgs e)
{
Window2 win2 = new Window2(1);
win2.Show();
}
Window2.xaml
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid Margin="0,0,170,249">
<TextBox Name="txtBox1" Margin="18,160,-18,-173"></TextBox>
<TextBox Name="txtBox2" Margin="18,119,-18,-134"></TextBox>
<TextBox Name="txtBox3" Margin="18,76,-18,-93"></TextBox>
<TextBox Name="txtBox4" Margin="18,36,-18,-50"></TextBox>
</Grid>
</Window>
Window2.xaml.cs
public partial class Window2 : Window
{
public Window2(int Id)
{
InitializeComponent();
ReadDataFromDB(Id);
}
public void ReadDataFromDB(int Id)
{
//read your data
txtBox1.Text = "Some value 1";
txtBox2.Text = "Some value 2";
txtBox3.Text = "Some value 3";
txtBox4.Text = "Some value 4";
}
}

Use Entity Framework.
Follow below tutorials..
http://blogs.msdn.com/b/wriju/archive/2010/06/09/ado-net-entity-framework-4-0-loading-data-in-4-ways.aspx
https://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx

1)Create ui elements in the window
2)Create a model class with the required fields.
3)While clicking the
button in window1 pass the id to the constructor of the next window.
4).Using the id query the database based on your need.Get the results
and bind the fields to the ui elements values.
5).These database querying and all can be written inside your
constructor or window load event.
You can accomplish this using MVVM.No need to write excess code for setting the values to the ui.

Related

WPF data binding is not working for second usage

I'm using GalaSoft.MvvmLight.Messaging for communication between classes.
From my ViewModel I'm asking for an object of type view to use it as the content of my DialogHost ( part of the MaterialDesign Framework ):
ViewModel:
Messenger.Default.Send(new NotificationMessage("dialogFormOpenStaple"));
-
private void DialogReceived(string msg, object param)
{
if (msg == "dialogFormOpenStaple")
{
this.dialogContent = param;
this.isDialogOpen = true;
}
}
View
private void NotificationMessageReceived(string msg)
{
if (msg == "dialogFormOpenStaple")
{
object content = new dialogEditMode();
Messenger.Default.Send(new NotificationMessage<object>(content, "dialogFormOpenStaple"));
}
}
All this is working fine. In my XAML itself I got some button bindings to RelayCommands.
<UserControl x:Class="kati2._0.production.dialogEditMode"
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:local="clr-namespace:kati2._0.production"
mc:Ignorable="d"
>
[...]
<Button Margin="8 0 0 0" Style="{DynamicResource MaterialDesignFlatButton}" Foreground="#FF757476" IsDefault="True" Command="{Binding dialogOpenStapleNo}">No</Button>
[...]
</UserControl>
Those RelayCommands are defined in my ViewModel. When I open die Dialog for the first time and click on the button it's working fine. If I try it a second time the dialog also opens up but the click event is not getting triggered.

How to blur parent window when child window pops up in wpf [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to implement this functionality on my WPF app. I want there to be like a darkened overlay/background to the entire screen(parent window) where the pop up window (child window) occurs so it will give the pop up window(child window) more visibility Just like the Image below. It is a browser popup window. Then when the pop up window(child window) is closed,the darkened overlay/background s removed.
Prior to launching your dialog, modify the Effect property of the parent window:
parentWindow.Effect = new BlurEffect();
When the dialog closes:
parentWindow.Effect = null;
For adding color to the overlay, you could work in layers (for simplicity, I am going the code-behind method; go MVVM/behavior if you have the time):
XAML:
<Window x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Grid">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label>Label</Label>
<TextBox Grid.Row="1"></TextBox>
<Button Click="ButtonBase_OnClick">Click</Button>
</Grid>
<Border x:Name="Splash" Grid.RowSpan="4" Opacity=".2" Visibility="Collapsed" Background="Black">
</Border>
</Grid>
</Window>
Code:
using System.Windows;
using System.Windows.Media.Effects;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Grid.Effect = new BlurEffect();
Splash.Visibility = Visibility.Visible;
var dlg = new Window();
dlg.ShowDialog();
Splash.Visibility = Visibility.Collapsed;
Grid.Effect = null;
}
}
}

How to generate GUI dynamically using WPF-MVVM

I am working on a wpf application strictly using MVVM. What i want to do is pop up a window which will pop up on a button click.
This window size will be determined dynamically depending upon the input that i have received from the pervious form submission on button click.
If the input given was 1 then on this window i will have GUI like: (where * represents the boundary)
********************************************
GUI : some textbox and buttons
********************************************
If the input given was 2 :
********************************************
GUI : some textbox and buttons
********************************************
********************************************
GUI : some textbox and buttons
********************************************
How to do this using MVVM approach ? Should i code for it using c# (because the input will be given dynamically)
What i have done is (tableView.xaml, on which will contain the GUI i shown you previously):
<UserControl x:Class="DBPythonAndXmlGenerator.View.tableView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
xmlns:local="clr-namespace:DBPythonAndXmlGenerator.ViewModel"
xmlns:converters="clr-namespace:DBPythonAndXmlGenerator.converters"
>
<UserControl.Resources>
<converters:Table x:Key="TableCoverter" />
</UserControl.Resources>
<Grid>
</Grid>
</UserControl>
And my button click function (MVVM) is below:
public ICommand SavePersonCommand
{
get
{
if (savePersonCommand == null)
savePersonCommand = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));
return savePersonCommand;
}
}
public bool SaveCanExecute()
{
return !string.IsNullOrEmpty(DB_Col.DbName) && !string.IsNullOrEmpty(DB_Col.NumTables);
}
public void SaveExecuted()
{
System.Windows.MessageBox.Show(string.Format("Saved: {0} {1} ", DB_Col.DbName, DB_Col.NumTables));
}
How to render data in this Grid dynamically using c# follwing MVVM If my approach is right?

Code behind Data Context is null, even though its values are showing on the page - Windows Phone 8.1 [duplicate]

This question already has answers here:
WPF User Control's DataContext is Null
(3 answers)
Closed 7 years ago.
I have a Page which contains a User Control, the Page's Data Context is bound, and so is the Contained User Control. When the Page is displayed, I can see that the User Control in the Page is displaying the values from the element as I would expect, such as Title and Description, however, in the Code Behind on the same User Control, the Data Context is null.. What am I doing wrong?
I need the Data Context so I could edit it's values within the control or change other UI elements within the page. What am I doing wrong here?
MyPage:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding}"
The use of the User Control with that page:
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
<local:ucFooControl DataContext="{Binding}"/>
</Grid>
Then, where I am seeing null in User Control Code Behind, even though display elements are showing bound items.
public ucFooControl()
{
this.InitializeComponent();
if (this.DataContext != null)
{
bar= (Bar)DataContext;
this.DoSomething((bar);
}
}
Try this:
public MyUserControl1()
{
this.InitializeComponent();
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (this.DataContext != null)
{
bar = (Bar)DataContext;
this.DoSomething((bar);
}
}

Pop context menu in textbox when # is input

I am trying to build a chat application, I would like to mimic facebook tag friends functionality. When the user types # in the textblock, I want to pop a context menu with a list of items. How can this be triggered in wpf mvvm app?
Example.
I would do it the following way :
Subscribe to the TextChanged event and whenever there is a change that contains # then show the popup, otherwise hide it.
Note that it tracks for new changes in the TextBox, therefore the popup will disappear as soon as the user presses another key or in your case when the user selected a user from the auto-completion you have provided in the pop-up.
User hasn't typed #
User just typed #
<Window x:Class="WpfApplication11.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>
<Popup x:Name="MyPopup" Placement="Center">
<Popup.Child>
<Border BorderBrush="Red" BorderThickness="1" Background="White">
<Grid>
<TextBlock>My popup</TextBlock>
</Grid>
</Border>
</Popup.Child>
</Popup>
<TextBox TextChanged="TextBoxBase_OnTextChanged" />
</Grid>
</Window>
Code behind:
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication11
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox) sender;
foreach (TextChange textChange in e.Changes)
{
string substring = textBox.Text.Substring(textChange.Offset, textChange.AddedLength);
if (substring == "#")
{
MyPopup.IsOpen = true;
}
else
{
MyPopup.IsOpen = false;
}
}
}
}
}
That said, you might want to further enhance it and integrate it properly your application ;-)

Categories

Resources