2

I need to access a control in XAML from a different class e.g. a text box called myTextBox. This is what I did but got an error:

   Windows1 w = new Windows1();
   w.myTextBox.text = "Hello!";

However, I'm getting an error message as:

'Set property 'System.Windows.Controls.ContentControl.Content' threw an exception.

The sticky error points to

<Grid Margin="10">
    <Grid.RowDefinitions> <!-- error points to this line -->
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

Please give me some direction to find the reason or error.

Thanks, Amit

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
amit kohan
  • 1,612
  • 2
  • 25
  • 47

3 Answers3

1

Something else is going on, because I was able get this to work in a simple application without doing anything tricky. So what else is in your scenario? Is your "different" class running in a background thread? If so you may need to use a dispatcher to get to the UI thread.

Nogusta
  • 909
  • 5
  • 10
  • I'm trying to access the control in a different class in a method called OnDrop() so when an object is dragged/dropped on canvas I must store its ID in a list and show it in a textbox. – amit kohan Jun 19 '12 at 00:13
1

Check this out: https://github.com/kaisellgren/Git-GUI/blob/master/Libraries/UIHelper.cs

It will find a Child of a given item in the visual tree.

Usage:

var recentCommitMessages = UIHelper.FindChild<ComboBox>(this, "RecentCommitMessages");

Here it will search all ComboBox elements for one with id "RecentCommitMessages"

stan
  • 4,885
  • 5
  • 49
  • 72
  • UIHelper is not recognized. Any idea? – amit kohan Jun 19 '12 at 06:29
  • OK, I included the class and always it returns null. – amit kohan Jun 19 '12 at 18:00
  • sure ` if (content != null) { newObject = new DroppedObject(); newObject.Content = content; //get the tag for now Type typeofDroppedOjbect = content.GetType(); Object o = t.GetProperty("Tag").GetValue(content, null); newItem.Tag = (string)o; var q = UIHelper.FindChild(this, "textSearchElement");` – amit kohan Jun 20 '12 at 16:32
  • Did you add the id "textSearchElement" to the textbox element? – stan Jun 20 '12 at 18:14
  • You must name the search element like `x:Name="textSearchElement`, and the first parameter to `FindChild` is the scope where to look for it. You passed `this`, are you sure that's what you want? Is `this` a user control or what is it? Try `UIHelper.FindChild(Application.Current.MainWindow, "textSearchElement")` instead. – Tower Jun 20 '12 at 18:17
  • Thanks I got the solution here [one solution](http://stackoverflow.com/questions/10722791/find-logical-child-including-hidden-and-collapsed-nodes) – amit kohan Jun 21 '12 at 16:52
0
w.Dispatcher.Invoke(new Action(() => { w.myTextBox.text = "Hello!"; }));
Aleksandr Vishnyakov
  • 1,872
  • 5
  • 23
  • 39
  • as soon as I type it in VS, it says: **Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type** – amit kohan Jun 19 '12 at 00:17
  • The error still is coming up for both cases of using `Dispatcher.Invoke()` – amit kohan Jun 19 '12 at 05:22
  • Thanks indeed for your comment but before run, VS complains as following: *System.Action does not contain a constructor that takes 0 arguments.* – amit kohan Jun 19 '12 at 06:38
  • Oh, I don't have access to the Visual Studio at the moment. I' sorry. Which version of the .NET Framework are you using? – Aleksandr Vishnyakov Jun 19 '12 at 08:24
  • You can also use: `w.Dispatcher.Invoke((Action) delegate() { w.myTextBox.text = "Hello!"; }));` or `w.Dispatcher.Invoke((Action) ({ w.myTextBox.text = "Hello!"; })));` – Aleksandr Vishnyakov Jun 19 '12 at 08:32
  • I appreciate your concern. I'm using VS2010 and .NET 4.0 platform. – amit kohan Jun 19 '12 at 19:31
  • before I use w.Dispatcher.Method() or anything I am instantiating an object as `window1 w = new window1();` this throwing error. why is that?! – amit kohan Jun 19 '12 at 19:35
  • I even changed it to `App.Current.MainWindow.Dispatcher.Invoke((Action)delegate() { App.Current.MainWindow.myTextbox.Text = "Hello!"; });` but myTextbox which is the ID for that control is not recognized. – amit kohan Jun 19 '12 at 19:38
  • Could this be related to some tagging issues in XAML file? because every time error message shows its stick line attached to XAML tag! which is `` – amit kohan Jun 19 '12 at 19:53
  • Try `Application.Current.Dispatcher.Invoke((Action)delegate() { var w = new Window1(); w.myTextBox.Text = "Hello World!"; });` – Aleksandr Vishnyakov Jun 20 '12 at 05:12
  • This still throws error and points to the XAML file content I had posted above. – amit kohan Jun 20 '12 at 16:51
  • OK the solution is http://stackoverflow.com/questions/10722791/find-logical-child-including-hidden-and-collapsed-nodes – amit kohan Jun 20 '12 at 18:45