I am reading in an XML file and using it to populate a WPF Form. Each entry in the XML file is used to create a button and a label.
The problem I am having is associating the button with the label:
Here is my code:
// Loop through Backups
foreach (var back in backups)
{
// Create Wrap Panel
myWrapPanel = new WrapPanel();
//myWrapPanel.Background = System.Windows.Media.Brushes.Red;
myWrapPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
myWrapPanel.Width = 600;
myWrapPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = System.Windows.VerticalAlignment.Top;
// **************************************
// Add Controls to Wrap Panel
// **************************************
// Backup Source
System.Windows.Controls.Button btnBackupSource = new System.Windows.Controls.Button();
btnBackupSource.Content = "Source";
btnBackupSource.Height = 25;
btnBackupSource.Width = 75;
btnBackupSource.Click += btnBackupSource_Click;
btnBackupSource.Name = "btnBackupSource";
myWrapPanel.Children.Add(btnBackupSource);
System.Windows.Controls.Label lblBackupSource = new System.Windows.Controls.Label();
lblBackupSource.Height = 25;
lblBackupSource.Width = 461;
lblBackupSource.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
lblBackupSource.VerticalAlignment = System.Windows.VerticalAlignment.Top;
lblBackupSource.Name = "lblBackupSource";
lblBackupSource.Content = "";
myWrapPanel.Children.Add(lblBackupSource);
The button and labels are created OK but I am struggling to associate them with each other. For example when a button is pressed the result needs to come up in the individual label which is associated with the button.
Any suggestions on how to proceed would be very welcome!
What I have currently got is each button updating a single label:
private void btnBackupSource_Click(object sender, RoutedEventArgs e)
{
SourceFolderBrowser = new System.Windows.Forms.FolderBrowserDialog();
// Allow users to creating new folders and default to the my documents folder.
SourceFolderBrowser.ShowNewFolderButton = true;
SourceFolderBrowser.RootFolder = Environment.SpecialFolder.Personal;
SourceFolderBrowser.ShowDialog();
label2.Content = SourceFolderBrowser.SelectedPath;
}
However what I am trying to do is get each button to update its associated label.
Here is my suggestion for you:
Store the label you want to associate with the button in the button’s
Tagproperty like this:Later when you to do something in the wired up
btnBackupSource_Clickyou can do it like this: