我正在尝试向PowerShell中的日期选择器WPF control
添加事件处理程序,以在用户选择日期时触发事件。我试过跟随,但它没有用。有谁知道在PowerShell中使用的日期选择器的正确事件处理程序是什么?
$dpDate.Add_SelectedDateChanged({ do-something-here })
代码段如下:
<DatePicker x:Name="dpDate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Focusable="false"/>
$dpDate.Add_SelectedDateChanged
({
$DateFormat = get-date $dpDate.SelectedDate -f yyy-MM-dd
$tb_FinalDate.text = $DateFormat
})
答案 0 :(得分:1)
这是执行您要求(经过测试和验证)的操作的Powershell / WPF代码:
| Valid | Age | a | b | c | d | e |
|-------|-----|---|---|---|---|---|
| 1 | 18 | 0 | 0 | 0 | 0 | 0 |
| 0 | 8 | 1 | 0 | 0 | 0 | 1 |
| 0 | 48 | 0 | 1 | 0 | 0 | 1 |
| 1 | 22 | 0 | 0 | 0 | 0 | 0 |
使用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)
#Turn XAML into PowerShell objects
$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'
})
#Display Form
$Window.ShowDialog() | Out-Null
时,每次选择日期时都会更新文本框的文本:
SelectedDateChanged