我包含了一个示例程序:似乎在进入 PasswordBox 时,输入文本并退出会导致 PasswordBox 变为空白。但是在 PasswordBox 中双击、输入文本和跳出不会。为什么会这样?
编辑:我发现这也发生在 TextBox 上,所以它不是 PasswordBox 特有的错误。
重新创建每个场景的步骤:
让密码消失
- 单击新建。
- 单击名字字段。
- 输入一些东西
- 按 TAB 键
- 输入一些东西
- 按 TAB 键
- 按 TAB 键(是两次,否则 Save 事件会触发)
- 输入一些东西
- 注意点而不是文本 -PasswordBox 正在工作!
- 按 TAB 键。
- 当 PasswordBox 变为空白时,让您的朋友大吃一惊!
使密码不消失
- 单击新建。
- 单击名字字段。
- 输入一些东西
- 按 TAB 键
- 输入一些东西
- 双击密码字段。
- 输入一些东西
- 注意点而不是文本 -PasswordBox 正在工作!
- 按 TAB 键。
- 让您的朋友大吃一惊……等等,PasswordBox 不是空白的吗?怎么回事?
示例代码:
using System;
using System.Collections.ObjectModel;
namespace WpfApplication1 {
public sealed class MyData {
private ObservableCollection<MyDataRow> dataList;
public ObservableCollection<MyDataRow> DataList { get { return dataList; } }
public MyData() { dataList = new ObservableCollection<MyDataRow>(); }
public void AddBlankRow() { DataList.Add(new MyDataRow(this)); }
}
public sealed class MyDataRow {
private readonly MyData myData;
public MyDataRow(MyData myData) { this.myData = myData; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
}
}
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication1 {
public partial class MainWindow : Window {
private MyData Data { get { return (MyData)DataContext; } }
public MainWindow() { InitializeComponent(); }
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) {
PasswordBox pb = (PasswordBox)sender;
if (pb != null) {
MyDataRow row = pb.DataContext as MyDataRow;
if (row != null) { row.Password = pb.Password; }
}
}
private void Window_Loaded(object sender, RoutedEventArgs e) { DataContext = new MyData(); }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { }
private void SaveExecute(object sender, ExecutedRoutedEventArgs e) { }
private void NewExecute(object sender, ExecutedRoutedEventArgs e) { Data.AddBlankRow(); }
private void CancelExecute(object sender, ExecutedRoutedEventArgs e) { Close(); }
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" Closing="Window_Closing">
<Window.CommandBindings>
<CommandBinding Command="Save" Executed="SaveExecute" />
<CommandBinding Command="New" Executed="NewExecute" />
<CommandBinding Command="Close" Executed="CancelExecute" />
</Window.CommandBindings>
<Grid Margin="0,10,0,0">
<DataGrid ItemsSource="{Binding DataList}" ColumnWidth="*" Margin="10,0,9,38" HorizontalAlignment="Stretch"
AutoGenerateColumns="False" GridLinesVisibility="Horizontal"
HeadersVisibility="Column" HorizontalGridLinesBrush="LightGray" CanUserReorderColumns="False" Background="White" >
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTemplateColumn Header="Password" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<PasswordBox PasswordChanged="PasswordBox_PasswordChanged" BorderThickness="0"
Height="23" HorizontalAlignment="Stretch"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button
Content="_New" Command="New"
Width="75" Height="23" Margin="10,10,10,10"
HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<Button
Content="_Save" Command="Save"
IsDefault="True"
IsEnabled="True"
Width="75" Height="23" Margin="10,10,91,10"
HorizontalAlignment="Right" VerticalAlignment="Bottom" />
<Button
Content="Cancel" Command="Close"
IsEnabled="True"
Width="75" Height="23" Margin="10,10,10,10"
HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
</Window>
编辑
使用 CellTemplate 和 EditingCellTemplate 的数据模板,我的工作就是保持一致 - 总是显示一些东西,(在这种情况下是一个省略号)。这样,无论如何,当 PasswordBox 失去焦点时,它会变回椭圆。我认为这比有时看到子弹字符有时看到空白要好。David Edey 的 DataGrid_PreparingCellForEdit 非常适合解决双标签问题。
这是我的数据模板。
<Grid.Resources>
<DataTemplate x:Key="PasswordTemplate" >
<Label BorderThickness="0" Height="23" HorizontalAlignment="Stretch" Content="..."/>
</DataTemplate>
<DataTemplate x:Key="EditingPasswordTemplate" >
<PasswordBox PasswordChanged="PasswordBox_PasswordChanged" BorderThickness="0"
Height="23" HorizontalAlignment="Stretch" />
</DataTemplate>
</Grid.Resources>