WPF 中PasswordBox控制元件的Password屬性不能Binding問題解決方法
最近用到了PasswordBox控制元件,但是發現Password屬性不能Binding,因為它不是依賴屬性,在網上找了找解決方法,自己做了小Demo,方便以後使用。
一、前臺檔案內容
<Window x:Class="PasswordBoxDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="142" Width="256"
xmlns:Helper="clr-namespace:PasswordBoxDemo"
WindowStartupLocation="CenterScreen">
<Grid>
<ListView x:Name="lvUsers"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
>
<ListView.View >
<GridView x:Name="GridView">
<GridView.Columns>
<GridViewColumn Header="使用者名稱" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBox
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Text="{Binding Path=UserName}"
ToolTip="{Binding Path=UserName}"
Width="100"
Height="20"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="密碼" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<PasswordBox
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="100"
Height="20"
PasswordChar="*"
MaxLength="20"
Helper:PasswordBoxHelper.Attach="True"
Helper:PasswordBoxHelper.Password="{Binding Path=Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
二、後臺內容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace PasswordBoxDemo
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<User> users = new ObservableCollection<User>();
User p = new User();
p.UserName = "張三";
p.Pwd = "zhangsan";
User p2 = new User();
p2.UserName = "李四";
p2.Pwd = "lisi";
users.Add(p);
users.Add(p2);
this.lvUsers.ItemsSource = users;
}
}
public class User
{
private string _userName;
private string _password;
public string UserName
{
set { _userName = value; }
get { return _userName; }
}
public string Pwd
{
set { _password = value; }
get { return _password; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace PasswordBoxDemo
{
/// <summary>
/// 為PasswordBox控制元件的Password增加繫結功能
/// </summary>
public static class PasswordBoxHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordBoxHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordBoxHelper));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}
}
四、截圖
相關推薦
WPF 中PasswordBox控制元件的Password屬性不能Binding問題解決方法
最近用到了PasswordBox控制元件,但是發現Password屬性不能Binding,因為它不是依賴屬性,在網上找了找解決方法,自己做了小Demo,方便以後使用。 一、前臺檔案內容 <Window x:Class="PasswordBoxDemo.MainWin
用winform實現的類似於WPF中PopUp控制元件的一段程式碼
用winform實現的類似於WPF中PopUp控制元件的一段程式碼 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using Syste
wpf 中DataGrid 控制元件的樣式設定及使用
本次要實現的效果為: 這個DataGrid需要繫結一個集合物件,所以要先定義一個Experience類,包含三個欄位 /// <summary> /// 定義工作經歷類 /// </summary> p
WPF中重疊控制元件的滑鼠穿透點選
Problem:有控制元件A和控制元件B,位置完全重疊,B覆蓋在A上方。換句話說,B只是A的修飾(實現中經常有可能會碰到這樣的情況,比如B可能是一個png圖片,你又不想重寫A的模板),我們需要在滑鼠操作時透過B直接點選到A。 Solution:你可能會想讓B的滑鼠事件不執行(e.handle=f
wpf中ListView控制元件點選列頭排序功能實現(超簡潔實用)
.xaml檔案中的主要程式碼 <ListView Name="lvMeasureData" GridViewColumnHeader.Click="Sort_Click" SelectionChanged="lvMeasureData_SelectionChanged"
WPF中TreeView控制元件資料繫結和後臺動態新增資料
資料繫結: TreeView資料繫結需要使用層次結構資料模板(HierarchicalDataTemplate)來顯示分層資料。XAML程式碼如下: <TreeView Name="chapterTree" Grid.Column="0"> <TreeVie
WPF 中RichTextBox控制元件用法細講
讀取RichTextBox的內容到string,將字串儲存到資料庫的方法就不寫了,大家都會 string GetTextByRichBox(RichTextBox box) {MemoryStream s = new MemoryStream();TextRange documentTextRange =
WPF 自定義控制元件依賴屬性怎麼實時變化?
WPF 自定義的依賴屬性要想在介面上能立即看到屬性變化的值。必須實現回撥通知 下面以最近剛自定義的RadioButton為例 public class RadioButton360 : RadioButton { public static r
wpf中datagrid 控制元件 隱藏表頭方法
預設情況下,顯示 DataGrid 列標題。 若要隱藏列標題,必須將 HeadersVisibility 屬性設定為 DataGridHeadersVisibility.Row 或 DataGridHeadersVisibility.None。 預設情況下,當顯示列標題
wpf中給控制元件設定樣式的三種方式
l 直接在控制元件下面寫樣式 <Grid> <Button Width="200"Height="30" Content="百度一下">
ASP.NET中 ValidationGroup[控制元件的屬性]:分組驗證的使用
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dome5.aspx.cs" Inherits="Dome5" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit
WPF中datePicker1控制元件獲取中的日期格試為YYYY-MM-DD 轉換成yyyMMdd格式
1.系統預設轉換前的格式 程式碼如下 textBox3.Text = datePicker1.Text; 2. //yyyyMMdd 代表要轉換的格式而且注意字母大小寫
WPF中DataGrid控制元件內Button的Command和CommandParameter的繫結
場景:視訊上傳功能,上傳列表使用DataGrid控制元件,視訊有不同的狀態對應不同的操作,DataGrid中最後一列為操作列,裡面是Button控制元件。希望點選Button後執行對應的操作,但是設定Button的 Command="{Binding VideoOperat
Android中EditText控制元件的幾種使用方法
2. MultiAutoCompleteTextView是AutoCompleteTextView的子類,它是對AutoCompleteTextView的擴充套件,但是你必須提供一個 MultiAutoCompleteTextView.Tokenizer來區分不同的字串,通過使用這個方法:set
Android中TextView控制元件的singleLine廢棄解決
在Android中想實現TextView的單行顯示,很簡單的一個方法是TextView中的singleLine設定為True即可,當文字內容超過單行的時候,就會在該行行尾部新增三個省略號代替。 顯示
跨執行緒訪問控制元件拋異常的解決方法
方法1:關閉跨執行緒操作的檢查,在建構函式裡面新增:System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;,但是此方法不推薦使用。 方法2:(推薦使用此方法)在訪問的地方找回建立控制元件的執
微信小程式swiper控制元件卡死的解決方法
微信小程式swiper控制元件,在使用過程中會偶爾出現卡死現象(不能再左右滑動),跟蹤一下,歸結原因可能是swiper元件內部問題,當無法響應使用者快速翻動動作時,當前頁變數current無法變更為正確頁碼索引,而是被置為0,所以,解決這個問題的思路如下:swiperchan
LinearLayout介面放置過多控制元件,導致下方控制元件不可見的解決方法:
小編在進行專案的程式碼編寫時,發現了該問題。最簡單的方法便是使用滾動條,通過滑動,實現對下方控制元件的操作。 通過對相對應的佈局xml檔案進行操作,在最外層的LinearLayout佈局外新增ScrollView。 程式碼實現: <?xml vers
C#控制元件的閃爍問題解決方法總結
最近對程式碼作了一些優化,試驗後效果還可以,但是發現介面會閃爍,具體是TreeView控制元件會閃爍,語言為C#,IDE為VS2005。在查閱一些資料,使用了一些基本技術後(如開啟雙緩衝),發現沒什麼效果。 於是使用Profiler工具,查找出瓶頸在於每
C#控制元件的閃爍問題解決方法
protected override void WndProc(ref Message m) { if (m.Msg == 0x0014) // 禁掉清除背景訊息