wpf - powershell datatemplate binding syntax -
i'm having problems figuring out syntax itemscontrol databinding in powershell. have simple wpf script below 2 examples of itemscontrol data template. first 1 (list01) has multiple elements , not show properly, 2nd itemscontrol (list02) has 1 binding element , works fine.
i'm looking correct syntax bind objects first itemscontrol (list01).
complete powershell script (:
[xml]$xaml = @' <window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:name="mainwindow" title="itemscontroldatabindingsample" height="350" width="300"> <grid margin="10"> <grid.rowdefinitions> <rowdefinition minheight="20"/> <rowdefinition minheight="50"/> <rowdefinition minheight="50"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="50"/> </grid.columndefinitions> <itemscontrol name="list01" grid.row="1"> <itemscontrol.itemtemplate> <datatemplate> <grid margin="5"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="100" /> </grid.columndefinitions> <textblock text="{binding title}" /> <progressbar grid.column="1" minimum="0" maximum="100" value="{binding completion}" /> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> <itemscontrol name="list02" grid.row="2"> <itemscontrol.itemtemplate> <datatemplate> <grid margin="5"> <grid.columndefinitions> <columndefinition width="*" /> </grid.columndefinitions> <label content="{binding}"/> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid> </window> '@ #new-object system.windows.controls.itemscontrol [void][system.reflection.assembly]::loadwithpartialname('presentationframework') [void][reflection.assembly]::loadwithpartialname('system.drawing') #read xaml $form = [windows.markup.xamlreader]::load( (new-object system.xml.xmlnodereader $xaml) ) #find objects $mainwindow = $form.findname('mainwindow') $list01 = $form.findname('list01') $list02 = $form.findname('list02') ## not work ## $source01 = @( [ordered]@{ title=([string]'complete wpf tutorial'); completion=([int]45) }, [ordered]@{ title=([string]'learn c#'); completion=([int]80) }, [ordered]@{ title=([string]'wash car'); completion=([int]25) } [ordered]@{ title=([string]'make kids homework'); completion=([int]3) } ); ## not work ## $source01 = @{ title='test01'; completion=50 } ## not work ## $testarray = @() $tmpobject = select-object -inputobject "" title,completion $tmpobject.title = 'complete wpf tutorial' $tmpobject.completion = 45 $testarray += $tmpobject $list01.itemssource = $testarray #$list01 | gm -type method ## works ## $source02 = @('test01','test02','test03') $list02.itemssource = $source02 [void]$form.showdialog();
this code based off simple c# example: http://www.wpf-tutorial.com/list-controls/itemscontrol
however, i'm not sure how syntax directly transates powershell.
i suggest using mvvm approach. set datacontext
property of window , bind itemscontrol.itemssource
property list want, e.g. itemssource="{binding myitemslistproperty}"
.
a full example of (based on example code posted above):
$xaml = @" <window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:name="mainwindow" title="itemscontroldatabindingsample" height="350" width="300"> <grid margin="10"> <itemscontrol itemssource="{binding myitemslistproperty}"> <itemscontrol.itemtemplate> <datatemplate> <grid margin="5"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="100" /> </grid.columndefinitions> <textblock text="{binding title}" /> <progressbar grid.column="1" minimum="0" maximum="100" value="{binding completion}" /> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid> </window> "@ [void][system.reflection.assembly]::loadwithpartialname('presentationframework') #read xaml $window = [windows.markup.xamlreader]::parse($xaml) $viewmodel = new-object psobject -property @{ myitemslistproperty = @( new-object psobject -property @{ title='complete wpf tutorial' completion=45.0 }; new-object psobject -property @{ title='learn c#' completion=80.0 }; new-object psobject -property @{ title='wash car' completion=25.0 }; new-object psobject -property @{ title='make kids homework' completion=3.0 }; ) }; $window.datacontext = $viewmodel $window.showdialog()
Comments
Post a Comment