My WPF application Interface demands something really different. Actually there are dynamic buttons in my application. Now i want that as i click on Button it's Panel should create and open to add something in it's textbox to save in database! On the same way Format TextBox should update with opened Panels as this image below is showing you all.
How to differentiate between panels textboxes to save text in their textboxes in database?
EDIT:
I am not using MVVM!
I hope you are just asking for a general approach to the problem, as I can't be bothered to code all that stuff.
So here it goes:
Any more questions, just ask :)
--EDIT--
1. As I have said, create a user control that will be your panel. This way you can instantiate it in the code (xaml.cs file) on a button-click event, just as you would instantiate object of any other class.
2. When you are creating your panel user-control, let's call it MyPanel
, add a property in the code behind and call it whatever you want (ParentButton
maybe?). Now you could add a constructor that would take the button ID as parameter, or you could simply use standard constructor and then assign the button ID to that property before adding the panel to the collection.
--EDIT--
Here is a link to my struggles with ObservableCollection. There you can find a quick and simple way to attach collections to Lists and handling changes.
Microsoft tutorial on INotifyPropertyChanged, you would implement that interface in your panel (probably)
Microsoft tutorial on ObservableCollections.
Microsoft tutorial on how to bind data to lists etc.
Now remember: they use simple objects in their tutorials, but you can use user-control in the same way.
Again, it is a lot of reading, but you gotta do it yourself... Good luck :)
--EDIT--
Actually, you don't even have to create a user control, just create an object with data you want to hold, like that:
class MyPanel: INotifyPropertyChanged
{
private string _id;
private string _text;
public string Id
{
get { return _id; }
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged();
}
}
}
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text= value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Then add a control to your app window, where you want to show your panels:
<ItemsControl Name="lstPanels" ScrollViewer.VerticalScrollBarVisibility="Auto">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel orientation="vertical">
<TextBlock Text="{Binding Id}">
<TextBox Text="{Binding Text, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Then add an observable collection:
private ObservableCollection<MyPanel> panels = new ObservableCollection<MyPanel>();
And in constructor attach it to your ItemControl:
lstPanels.ItemsSource = panels;
Then add a function to add a new panel:
private void addPanel(string buttonId)
{
MyPanel p = new MyPanel { Id = buttonId; };
panels.add(p);
}
Then on your button1/button2/button3 click call that function with appropriate id:
addPanel("button1");
addPanel("button2");
When you want to finish and loop through panels, use standard foreach or for loop on panels
:)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments