我有两个文本框。我想将第一个文本框镜像到第二个文本框,但是如果您在镜像文本后手动更新第二个框,则无论您是否再次更新第一个文本框,它都应保持不变。
<TextBox x:Name="TextBoxUserLogonNameUPN" HorizontalAlignment="Left" Height="23" Margin="10,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="300"/>
<TextBox x:Name="TextBoxUserLogonNameSamAccountName" HorizontalAlignment="Left" Height="23" Margin="326,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Text="{Binding Path=Text, ElementName=TextBoxUserLogonNameUPN, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
我已经尝试了上面的方法,但是在Binding中使用Mode = OneWay。如果我第二次更新第一个文本框,则在手动更新第二个文本框后,它将擦除第二个文本框并再次镜像。
我正在使用WPF和PowerShell。如果无法在Xaml中完成,我很乐意通过Powershell进行管理。
答案 0 :(得分:1)
假设textbox1
丢失focus
是应将textbox1
文本复制到textbox2
的事件,那么此代码应该对您有用。所有动作都在函数textBox1_LostFocus
中。第一次使用时,只需将textbox2
Tag
属性设置为您要通过第二次及以后测试的值即可
注意:该表单有两个标签和两个文本框
WpfWindow1.xaml.ps1是该程序的入口点
WpfWindow1.xaml.ps1
function Add-ControlVariables {
New-Variable -Name 'textBox1' -Value $window.FindName('textBox1') -Scope 1 -Force
New-Variable -Name 'textBox2' -Value $window.FindName('textBox2') -Scope 1 -Force
}
function Load-Xaml {
[xml]$xaml = Get-Content -Path $PSScriptRoot\WpfWindow1.xaml
$manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
$manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
$xaml.SelectNodes("//*[@x:Name='textBox1']", $manager)[0].RemoveAttribute('TextChanged')
$xaml.SelectNodes("//*[@x:Name='textBox2']", $manager)[0].RemoveAttribute('TextChanged')
$xaml.SelectNodes("//*[@x:Name='textBox1']", $manager)[0].RemoveAttribute('LostFocus')
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
[Windows.Markup.XamlReader]::Load($xamlReader)
}
function Set-EventHandlers {
$textBox1.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox1_TextChanged($sender,$e)
})
$textBox2.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
$textBox1.add_LostFocus({
param([System.Object]$sender,[System.Windows.RoutedEventArgs]$e)
textBox1_LostFocus($sender,$e)
})
}
$window = Load-Xaml
Add-ControlVariables
Set-EventHandlers
function textBox1_TextChanged
{
param($sender, $e)
#do stuff you want to do when textBox1.Text changes
}
function textBox2_TextChanged
{
param($sender, $e)
#do stuff you want to do when textBox2.Text changes
}
function textBox1_LostFocus
{
param($sender, $e)
if ($textBox2.Tag -ne "mirrored")
{
#turn off textBox2 TextChanged event so that code in textBox2_TextChanged is not executed
$textBox2.remove_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
$textBox2.Text = $sender.Text
$textBox2.Tag = "mirrored"
#turn textBox2 TextChanged event back on
$textBox2.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
}
}
$window.ShowDialog()
WpfWindow1.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="29" Margin="79,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="132" TextChanged="textBox1_TextChanged" LostFocus="textBox1_LostFocus"/>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="29" Margin="298,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="105" TextChanged="textBox2_TextChanged"/>
<Label Content="Text1:" HorizontalAlignment="Left" Height="25" Margin="32,45,0,0" VerticalAlignment="Top" Width="63"/>
<Label Content="Text2:" HorizontalAlignment="Left" Height="25" Margin="242,45,0,0" VerticalAlignment="Top" Width="56"/>
</Grid>
</Window>