这是用于执行所请求操作的Powershell/WPF代码(经过测试和验证):
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[xml]$xaml=@"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
Title="MainWindow" Height="265" Width="371">
<Grid>
<DatePicker x:Name="dpDate" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="tb_FinalDate" HorizontalAlignment="Left" Height="23" Margin="10,55,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="149"/>
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}
$dpDate.Add_SelectedDateChanged({
$tb_FinalDate.Text = Get-Date($dpDate.Text) -Format 'yyyy-MM-dd'
})
$Window.ShowDialog() | Out-Null
使用时
SelectedDateChanged
,则每次选择日期时都会更新文本框的文本:
$dpDate.Add_SelectedDateChanged({
$tb_FinalDate.Text = Get-Date($dpDate.Text) -Format 'yyyy-MM-dd'
})