欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法

 更新時(shí)間:2021年06月24日 11:00:25   作者:louzi  
有的時(shí)候會(huì)遇見PasswordBox的Password屬性不是依賴屬性,因此無(wú)法進(jìn)行數(shù)據(jù)綁定。本文介紹如何通過(guò)添加附加屬性解決該問(wèn)題,有此問(wèn)題的同學(xué)可以參考下本文

本文介紹下PasswordBox進(jìn)行數(shù)據(jù)綁定的方法,本文參考鏈接。

本文完整示例程序見GitHub。

問(wèn)題描述

PasswordBox的Password屬性不是依賴屬性,因此無(wú)法進(jìn)行數(shù)據(jù)綁定。

解決辦法

該問(wèn)題的解決辦法有多種,本文介紹如何通過(guò)添加附加屬性解決該問(wèn)題。

附加屬性是說(shuō)一個(gè)屬性本不屬于某個(gè)對(duì)象,但由于某種需求附加到該對(duì)象上,通過(guò)附加屬性可以實(shí)現(xiàn)將屬性與宿主解耦的目的。附加屬性本質(zhì)上就是依賴屬性,只是它們?cè)趯傩园b器和注冊(cè)時(shí)有區(qū)別。注冊(cè)附加屬性使用RegisterAttached方法,注冊(cè)依賴屬性使用Register方法,這兩個(gè)方法的參數(shù)差別并不大。

首先添加一個(gè)PasswordBoxBindingHelper類,該類包含一個(gè)附加屬性(snippet:propa+兩次tab),通過(guò)設(shè)置該屬性的PropertyChangedCallback將改變通知到PasswordBox.Password,并通過(guò)添加對(duì)PasswordBox.PasswordChanged事件的響應(yīng)來(lái)響應(yīng)PasswordBox.Password的改變。有了該附加屬性,即可進(jìn)行數(shù)據(jù)綁定。

public static string GetPasswordContent(DependencyObject obj) => (string)obj.GetValue(PasswordContentProperty);

public static void SetPasswordContent(DependencyObject obj, string value) => obj.SetValue(PasswordContentProperty, value);

public static readonly DependencyProperty PasswordContentProperty =
    DependencyProperty.RegisterAttached("PasswordContent", typeof(string), typeof(PasswordBoxBindingHelper),
    new PropertyMetadata(string.Empty, OnPasswordContentPropertyChanged));

private static void OnPasswordContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var box = d as PasswordBox;
    box.PasswordChanged -= OnPasswordChanged;
    var password = (string)e.NewValue;
    if (box != null && box.Password != password)
        box.Password = password;
    box.PasswordChanged += OnPasswordChanged;
}

private static void OnPasswordChanged(object sender, RoutedEventArgs e)
{
    var box = sender as PasswordBox;
    SetPasswordContent(box, box.Password);
}

然后在View中使用該附加屬性進(jìn)行數(shù)據(jù)綁定,本文示例中主窗口包含一個(gè)PasswordBox控件及一個(gè)Button按鈕:

// xaml 綁定附加屬性
<Window ...
        xmlns:local="clr-namespace:PasswordBoxBinding"
        Title="PasswordBoxBinding" Height="300" Width="450" WindowStartupLocation="CenterScreen">

    <Grid>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <PasswordBox MinWidth="200" Height="30" BorderBrush="LightGray" BorderThickness="2"
                         local:PasswordBoxBindingHelper.PasswordContent="{Binding Password,Mode=TwoWay}"/>
            <Rectangle Width="20"/>
            <Button Width="80" Height="30" Content="查看密碼" Command="{Binding ClickedCommand}"/>
        </StackPanel>
    </Grid>
</Window>

//xaml.cs 設(shè)置綁定源
public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MainWindowViewModel();
}

最后創(chuàng)建ViewModel進(jìn)行邏輯處理:

// ViewModel
public class MainWindowViewModel : INotifyPropertyChanged
{
    public string Password
    {
        get => _password;
        set
        {
            _password = value;
            OnPropertyChanged();
        }
    }

    public DelegateCommand ClickedCommand => _clickedCommand ?? (_clickedCommand = new DelegateCommand { ExecuteAction = OnClicked });

    // 使用CallerMemberName特性簡(jiǎn)化代碼,并可以避免手動(dòng)輸入錯(cuò)誤
    public void OnPropertyChanged([CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

    private void OnClicked(object o) => MessageBox.Show($"password: {Password}");

    public event PropertyChangedEventHandler PropertyChanged;

    private DelegateCommand _clickedCommand;
    private string _password;
}

// 實(shí)現(xiàn)ICommand
public class DelegateCommand : ICommand
{
    public bool CanExecute(object parameter) => CanExecuteAction?.Invoke(parameter) ?? true;

    public void Execute(object parameter) => ExecuteAction?.Invoke(parameter);

    public event EventHandler CanExecuteChanged;

    public Action<object> ExecuteAction { get; set; }
    public Func<object, bool> CanExecuteAction { get; set; }
}

以上就是WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法的詳細(xì)內(nèi)容,更多關(guān)于WPF PasswordBox數(shù)據(jù)綁定的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例

    C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例

    這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能,涉及C#界面布局、事件響應(yīng)及數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法

    C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法

    經(jīng)常會(huì)遇到一種情況,即當(dāng)拖動(dòng)一個(gè)窗體(主窗體)時(shí),其他窗體(子窗體)隨著該窗體移動(dòng),當(dāng)拖動(dòng)子窗體時(shí),其他窗體將不跟隨移動(dòng),這就是磁性窗體,所以本文給大家介紹了C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2024-04-04
  • C#導(dǎo)出數(shù)據(jù)到excel如何提升性能

    C#導(dǎo)出數(shù)據(jù)到excel如何提升性能

    這篇文章主要介紹了C#導(dǎo)出數(shù)據(jù)到excel如何提升性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • C#獲取文件相關(guān)信息的方法

    C#獲取文件相關(guān)信息的方法

    這篇文章主要介紹了C#獲取文件相關(guān)信息的方法,涉及C#中FileInfo類的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換

    C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換

    這篇文章介紹了C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#讀取XML中元素和屬性值的實(shí)現(xiàn)代碼

    C#讀取XML中元素和屬性值的實(shí)現(xiàn)代碼

    用C#讀取xml有很多方式,這里我就先使用XmlDocument讀取Xml,用一段代碼遍歷所有元素,并打印student的所有屬性和子節(jié)點(diǎn)的值
    2013-04-04
  • C#程序員最易犯的編程錯(cuò)誤

    C#程序員最易犯的編程錯(cuò)誤

    這篇文章主要介紹了C#程序員最易犯的10個(gè)編程錯(cuò)誤,了解這些錯(cuò)誤能夠更好地學(xué)習(xí)C#程序設(shè)計(jì),感興趣的小伙伴們可以參考一下
    2015-11-11
  • c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法

    c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法

    這篇文章主要介紹了c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • C#敏感詞過(guò)濾實(shí)現(xiàn)方法

    C#敏感詞過(guò)濾實(shí)現(xiàn)方法

    這篇文章主要介紹了C#敏感詞過(guò)濾實(shí)現(xiàn)方法,涉及C#針對(duì)字符串操作的常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C#中Web.Config加密與解密的方法

    C#中Web.Config加密與解密的方法

    C#中Web.Config加密與解密的方法,需要的朋友可以參考一下
    2013-04-04

最新評(píng)論