0

I'm using Visual Studio 2013

So for the most part I'm getting an error when I run the code below, the code is an attempt to create a label once a button is clicked, the label's content comes from a textbox called contentEventInput.

The error comes after I click the button to create the label.

An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll

private void btnEvent_Click(object sender, RoutedEventArgs e)
{    
    Label eventCreateText = new Label();
    eventCreateText.Foreground = new SolidColorBrush(Colors.White);
    eventCreateText.Content = contentEventInput;
    eventCreateText.Margin = new Thickness(0, 440, 836, 40);
    ephGrid.Children.Add(eventCreateText);
}

Do any of you know how to fix this?

daniele3004
  • 13,072
  • 12
  • 67
  • 75
user3708761
  • 275
  • 1
  • 6
  • 14
  • What control is `ephGrid` ? – Ghasem Sep 03 '14 at 04:51
  • 2
    use this before adding the control. **ephGrid.SetColumn(eventCreateText, 0); ephGrid.SetRow(eventCreateText, 0);** – a d Sep 03 '14 at 04:57
  • @MehdiKhademloo I get an error when attempting this. Member'System.Windows.Controls.Grid.SetRow(System.Windows.UIElement, int)' cannot be accessed with an instance reference; qualify it with a type name instead – user3708761 Sep 03 '14 at 05:20
  • 2
    it is usually called as `Grid.SetColumn(eventCreateText, 0)`. but I dont see that the issue is related to `System.ArgumentException`. there is something else, do try comment the code and see what happens. speially this line `eventCreateText.Content = contentEventInput;` just hoping there is no recursion involved. – pushpraj Sep 03 '14 at 06:10
  • add more code to describe all of problem – a d Sep 03 '14 at 06:13
  • @pushpraj Sorry, yeah that error still persists, but commenting out eventCreateText.Content = contentEventInput; does stop it. – user3708761 Sep 03 '14 at 06:28
  • what is `contentEventInput`? how do you create/retrieve it? – pushpraj Sep 03 '14 at 06:34
  • contentEventInput is a textbox element I added. I assumed once you added an element it was defined elsewhere. Should I define it here? – user3708761 Sep 03 '14 at 06:36
  • 4
    It is strongly recommended that you use proper XAML and DataBinding instead of procedural code to do whatever you're trying to do. You can't go around messing with the Visual Tree nesting existing elements into each other in WPF. The Visual Tree is a complex structure with complex behavior and life cycles and this sort of code is highly discouraged in WPF due to being prone to all sorts of runtime errors. – Federico Berasategui Sep 03 '14 at 06:38
  • @HighCore This is the first time I've heard this, just kind of assumed XAML was there for more precision and power over editing your elements... Where would you recommend I begin, just started WPF from WinForm the other day. All I'm really trying to do is to get this button to create a label form nothing, so I can drag and drop it wherever I please, bad idea? – user3708761 Sep 03 '14 at 06:41
  • 2
    @user3708761 there are several online tutorials explaining the differences between winforms and WPF. I can point you to Rachel's [Excellent Explanation](http://stackoverflow.com/questions/15681352/transitioning-from-windows-forms-to-wpf/15684569#15684569) about it. Also, if you need to dynamically create UI elements what you will really need is an [ItemsControl](http://drwpf.com/blog/itemscontrol-a-to-z/). WPF is very different from winforms in that it is really data-centric and thus most of the time you operate on your data rather than the UI elements. – Federico Berasategui Sep 03 '14 at 07:05

1 Answers1

0

You said:

commenting out eventCreateText.Content = contentEventInput; does stop [the error].

Therefore, I believe that your problem is caused because you attempted to add a UI element that already exists in the UI into another control in the UI. This is not allowed, as in WPF, each control can only be in the visual tree in one place at a time. If this is correct, you should have had an error saying something similar to what I just said.

The solution is to just copy the content, so in your case, you could add a new TextBox element and just copy the value of the Text property from your existing TextBox to the new one.

Sheridan
  • 68,826
  • 24
  • 143
  • 183