WPF 中的变量与 PropertyChanged 绑定

问题描述 投票:0回答:1

我在 PowerShell 脚本中实现了 WPF 接口。 当窗口打开时,将显示变量 MyFolder.SelectedFolder 的值。 但是当修改值时,并没有更新。

Class cFolder {
    [String] $Path
    [String] $SelectedFolder
} 

$MyFolder = [cFolder]::new()
$MyFolder.Path = "C:\temp"
$MyFolder.SelectedFolder = "temp"

<Label x:Name ="Lbl_SelFolder" Content="{Binding Path, UpdateSourceTrigger=PropertyChanged}"/>

$XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
$XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)
$XMLForm.DataContext = $MyFolder
$LblSelFolder = $XMLForm.FindName('Lbl_SelFolder')

感谢您的帮助

wpf powershell data-binding propertychanged
1个回答
0
投票

问题是没有人观察您稍后对

$MyFolder
对象的属性所做的更改。

解决此问题的一种方法是将对象存储在

System.Collections.ObjectModel.ObservableCollection<T>
实例中,并将后者分配给 WPF 表单(窗口)的
.DataContext
属性:

$XMLForm.DataContext = 
  [System.Collections.ObjectModel.ObservableCollection[cFolder]] $MyFolder

然后根据

$MyFolder
的更新触发标签控件的更新:

  • 更新

    $MyFolder
    的数据绑定属性,例如
    .SelectedFolder

    $MyFolder.SelectedFolder = 'tempNEW'
    
  • 然后将

    $MyFolder
    重新分配给可观察集合,作为其唯一的元素;这就是触发标签更新的原因:

    $XMLForm.DataContext[0] = $MyFolder
    

一个独立的示例,它在循环中更新数据上下文对象的 .SelectedFolder

 属性,以演示标签已响应更新:

using namespace System.Windows using namespace System.Windows.Data using namespace System.Windows.Controls using namespace System.Windows.Markup using namespace System.Xml using namespace System.Collections.ObjectModel # Note: `using assembly PresentationFramework` works in Windows PowerShell, # but seemingly not in PowerShell (Core) as of v7.3.5 Add-Type -AssemblyName PresentationFramework Add-Type -AssemblyName System.Windows.Forms Class cFolder { [String] $Path [String] $SelectedFolder } $MyFolder = [cFolder]::new() $MyFolder.Path = "C:\temp" $MyFolder.SelectedFolder = "temp" # Define the XAML document defining the WPF form with a single label. [xml] $xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Label data-binding demo" Height="80" Width="350" > <Label x:Name ="Lbl_SelFolder" Content="{Binding Path=SelectedFolder}"/> </Window> "@ # Parse the XAML, which returns a [System.Windows.Window] instance. $form = [XamlReader]::Load([XmlNodeReader] $xaml) # Use an observable collection as the data context. # with $MyFolder as the first and only element. $form.DataContext = [ObservableCollection[cFolder]] $MyFolder # Show the window non-modally and activate it. $null = $form.Show() $form.Activate() # While the window is open, process pending GUI events # and update the selected folder. $i = 0 while ($form.IsVisible) { # Note: Even though this is designed for WinForms, it works for WPF too. [System.Windows.Forms.Application]::DoEvents() # Sleep a little. Start-Sleep -Milliseconds 100 # Update the selected folder. $MyFolder.SelectedFolder = 'temp' + ++$i # It is through *re-assigning* the object as the first (and only) # element of the observable collection that the control update # is triggered. $form.DataContext[0] = $MyFolder }
    
© www.soinside.com 2019 - 2024. All rights reserved.