Use PowerShell to quickly bind WPF's ListBox data source

Source: Internet
Author: User

Sometimes, we do development in a closed environment, and do not have so many convenient development tools, can only use the current system comes with the basic tools to do development,

Using a powerful framework, for example, PowerShell is a great development tool that allows you to do most of the work without installing Visual Studio.

The following is a simple example of the binding of a ListBox data source in WPF

1. With regard to datasets, I don't do a long explanation, simply put, is the collection of one or more DataTable objects.

With PowerShell, it's a simple thing to do.

#Create Table Object

$table = New-object System.Data.Datatable "newtable"

$dataset = New-object System.Data.DataSet

#Define Columns

$col 1 = New-object system. Data.datacolumn "ContactName", ([string])

$col 2 = New-object system. Data.datacolumn "Address", ([string])

$col 3 = New-object system. Data.datacolumn "City", ([string])

$col 4 = New-object system. Data.datacolumn "Country", ([string])

#Add the Columns

$table. Columns.Add ($col 1)

$table. Columns.Add ($col 2)

$table. Columns.Add ($col 3)

$table. Columns.Add ($col 4)

#Create a row

foreach ($k in 1..5) {

$row = $table. NewRow ()

$row. ContactName = "Contactname$k"

$row. Address = "Address$k"

$row. City = "City$k"

$row. Country = "Country$k"

$table. Rows.Add ($row)

}

$dataset. Merge ($table)

So the DataSet object is created.

2. Create a new MainWindow.xaml file, simply put a UI element in the ListBox and control template.

Note that ItemsSource is a newtable table that binds a dataset.

<window
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
X:name= "Window" title= "Datagridwindow" height= "width=" >

<Window.Resources>
<datatemplate x:key= "Listboxtemplate" >
<stackpanel margin= "3" >
<dockpanel >
<textblock fontweight= "Bold" text= "Name:"
Dockpanel.dock= "left"
margin= "5,0,10,0"/>
<textblock text= ""/>
<textblock text= "{Binding ContactName}" foreground= "Green" fontweight= "Bold"/>
</DockPanel>

<dockpanel >
<textblock fontweight= "Bold" text= "Address:" Foreground = "Darkorange"
Dockpanel.dock= "left"
margin= "5,0,5,0"/>
<textblock text= "{Binding Address}"/>
<textblock text= ","/>
<textblock text= "{Binding city}"/>
<textblock text= ","/>
<textblock text= "{Binding country}"/>
</DockPanel>

</StackPanel>
</DataTemplate>
</Window.Resources>

<Grid>

<listbox margin= "17,8,15,26" name= "ListBox1"
Itemssource= "{Binding newtable}" itemtemplate= "{StaticResource listboxtemplate}"/>

</Grid>

</Window>

3. Then start to integrate, mainly to the context of the ListBox to assign values.

$XAML = [XML] (get-content MainWindow.xaml)

$reader = (new-object System.Xml.XmlNodeReader $xaml)

$Window =[windows.markup.xamlreader]::load ($reader)

$listBox 1 = $Window. FindName ("ListBox1")

$listBox 1.DataContext = $dataset

$Window. Showdialog () | Out-null

OK, so it should be OK.

However, before running, the first to determine whether to run under STA mode, because the running GUI is to run in STA

$host. Runspace.apartmentstate-eq ' STA '

If not, add Powershell-sta to it.

I am so running, new start.bat, inside content Powershell-sta.

After double-clicking, then do the following

Get the following results

Use PowerShell to quickly bind WPF's ListBox data source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.