1

我使用 PasswordHelper 类将密码框与视图模型绑定,但在提交时属性字段仍然为空。我尝试使用调试器,但属性没有获得价值。

XAML 代码..

<Page x:Class="ChatApp.View.Register"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  xmlns:se="http://schemas.microsoft.com/expression/2010/interactions"
  xmlns:vm="clr-namespace:ChatApp.ViewModel"
  xmlns:w="clr-namespace:ChatApp"
  mc:Ignorable="d" 
  d:DesignHeight="347" d:DesignWidth="350"
Title="Register">
<Page.DataContext>
    <vm:RegisterViewModel></vm:RegisterViewModel>
</Page.DataContext>
    <Grid Height="317">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="63" />
        <RowDefinition Height="32" />
        <RowDefinition Height="42*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="112*"/>
        <ColumnDefinition Width="188*" />
    </Grid.ColumnDefinitions>

    <Label Content="User ID :" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label1" VerticalAlignment="Top" />
    <Label Content="Password :" Grid.Row="2" Height="28" HorizontalAlignment="Left" Margin="12,2,0,0" Name="label2" VerticalAlignment="Top" />
    <Label Content="Name" Grid.Row="3" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label3" VerticalAlignment="Top" />
    <Label Content="Email ID :" Grid.Row="4" Height="28" HorizontalAlignment="Left" Margin="12,2,0,0" Name="label4" VerticalAlignment="Top" />
    <Label Content="Contact No :" Grid.Row="5" Height="29" HorizontalAlignment="Left" Margin="12,1,0,0" Name="label5" VerticalAlignment="Top" />
    <Label Content="Address :" Grid.Row="6" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label6" VerticalAlignment="Top" />
    <Label Content="Designation :" Grid.Row="7" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label7" VerticalAlignment="Top" />
    <TextBox Text="{Binding userID}" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="0,3,0,0" Name="txtID" VerticalAlignment="Top" Width="207" />
    <TextBox Grid.Column="1" Text="{Binding password}"  Grid.Row="2" Height="23" HorizontalAlignment="Left" Name="txtpassword" VerticalAlignment="Top" Width="207" Margin="0,2,0,0" />
    <TextBox Text="{Binding name}" Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Name="txtName" VerticalAlignment="Top" Width="207" Margin="0,2,0,0" />
    <TextBox Text="{Binding emailID}" Grid.Column="1" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="0,2,12,0" Name="txtEmail" VerticalAlignment="Top" Width="207" />
    <TextBox Text="{Binding contact}" Grid.Column="1" Grid.Row="5" Height="23" HorizontalAlignment="Left" Name="txtContact" VerticalAlignment="Top" Width="207" Margin="0,2,0,0" />
    <TextBox Text="{Binding address}" Grid.Column="1" Grid.Row="6" Height="56" HorizontalAlignment="Left" Margin="0,2,0,0" Name="txtAddress" VerticalAlignment="Top" Width="207" TextWrapping="Wrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" />
    <TextBox Text="{Binding designation}" Grid.Column="1" Grid.Row="7" Height="23" HorizontalAlignment="Left" Name="txtDesignation" VerticalAlignment="Top" Width="207" Margin="0,1,0,0" />        
    <TextBlock Height="28" HorizontalAlignment="Left" Margin="49,1,0,0" Name="textBlock1" Text="New Registration" VerticalAlignment="Top" Width="169" Grid.ColumnSpan="2" FontWeight="Bold" FontSize="20" />
    <Button Content="Register" Grid.Row="8" Height="23" HorizontalAlignment="Left" Margin="33,5,0,0" Name="btnSubmit" VerticalAlignment="Top" Width="75" Grid.Column="1">
        <i:Interaction.Triggers>
            <i:EventTrigger  SourceObject="{Binding ElementName=btnSubmit}" EventName="Click">
                <se:CallMethodAction MethodName="SubmitDetail" TargetObject="{Binding DataContext, ElementName=btnSubmit}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

</Grid>

这是我的密码助手类。

  public static class PasswordHelper
{
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password",
        typeof(string), typeof(PasswordHelper),
        new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

    public static readonly DependencyProperty AttachProperty =
        DependencyProperty.RegisterAttached("Attach",
        typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

    private static readonly DependencyProperty IsUpdatingProperty =
       DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
       typeof(PasswordHelper));


    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);
    }
}
4

1 回答 1

0

我发现您的代码有两个问题:

1.

XAML 在绑定路径方面区分大小写,但您在 XAML 中使用了小写的属性名称。(您可以从MSDN上XAML 概述 (WPF)页面的“XAML 中的大小写和空格”部分了解更多信息。

XAML 通常是区分大小写的。为了解析支持类型,WPF XAML 按照与 CLR 区分大小写相同的规则区分大小写。当按名称与程序集中的基础类型或类型的成员进行比较时,必须使用敏感大小写来指定对象元素、属性元素和属性名称。

2.

您不需要在处理程序中设置 的Password属性,尽管这不会导致您描述的问题。PasswordBoxOnPasswordPropertyChanged

于 2013-09-02T09:19:22.870 回答