Jason Shave
Jason Shave

Reputation: 2672

How to perform data binding of WPF/XAML using PowerShell

I have several PowerShell modules I've developed using WPF/XAML and one of them uses an example I found and used to bind a DataGrid ItemSource property to a variable called "resultsData":

<DataGrid Name="gridResults" ItemsSource="{DynamicResource resultsData}"/>

Then in the Window.Resources section of my XAML file there is a reference made to the data binding value:

<Window.Resources> <x:Array x:Key="resultsData" Type="sys:Object"/> </Window.Resources>

Finally, in PowerShell I can update the DataGrid's property as follows:

$uiHash.Window.Resources["resultsData"] = $uiHash.resultsHash

This works really well and I'm trying to replicate it in another project where I'm simply trying to bind the text property of a TextBlock using the Windows.Resources section in my XAML file. The problem is I have no idea what to put in that section to make it work.

I've tried using the same <x:Array> tag but it's obviously not an array. I've tried <x:Code> and <x:Null> and a multitude of other options which all result in a failure to load the window along with Visual Studio's visual designer barfing...

Could someone help me understand what to put in the Windows.Resources section to ensure I can reference these properties in PowerShell through the Window.Resources object?

Upvotes: 0

Views: 2121

Answers (1)

mm8
mm8

Reputation: 169200

The Text property of a TextBlock can be set to or bind to a string which is defined like this in XAML:

<sys:String x:Key="textResource">some text...</sys:String>

...where sys is mapped to the System namespace:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Upvotes: 1

Related Questions