ride_wind
Topic Author
Posts: 34
Joined: 07 Feb 2018, 03:33

PasswordBox Set password value

02 Apr 2018, 09:42

Hi

I hava a password and set it's password property value in xaml.
But the box is empty in UE4 and the password is correct using c++ code get.

Thanks.
 
User avatar
hcpizzi
Site Admin
Posts: 228
Joined: 09 Feb 2012, 12:40

Re: PasswordBox Set password value

02 Apr 2018, 16:20

Hi, could you please paste the XAML here?

Thank you.
 
ride_wind
Topic Author
Posts: 34
Joined: 07 Feb 2018, 03:33

Re: PasswordBox Set password value

03 Apr 2018, 04:54

Hi, could you please paste the XAML here?

Thank you.
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="200"/>
			<RowDefinition Height="100"/>
		</Grid.RowDefinitions>
		<PasswordBox x:Name="PasswordBox" Width="200" Height="30" Password="111" FontSize="20"/>
		<Button Grid.Row="1" Width="170" Height="30" Content="Show Password" FontSize="20"
				Command="{Binding Show}" CommandParameter="{Binding ElementName=PasswordBox}"/>
	</Grid>
PasswordBoxTest.gif
This is my test code.

I want to bind Password Property in passwordbox using AttachProperty,
But I found the PasswordBox showing length Is random.

Xaml code
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="200"/>
			<RowDefinition Height="100"/>
		</Grid.RowDefinitions>
		<PasswordBox x:Name="PasswordBox" Width="200" Height="30" FontSize="20"
					 ex:PasswordHelper.Attach="True"
					 ex:PasswordHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
		<Button Grid.Row="1" Width="170" Height="30" Content="Show Password" FontSize="20"
				Command="{Binding Show}"/>
	</Grid>
PasswordHelper class like this in C#
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);
		}
	}
this code work well in wpf , but it is error in UE4
Detail to see image.
PasswordBox.gif
sorry for i can't uploade the gif image, because it is to bigger.

Thanks.
 
User avatar
hcpizzi
Site Admin
Posts: 228
Joined: 09 Feb 2012, 12:40

Re: PasswordBox Set password value

07 Apr 2018, 15:10

I've modified your sample slightly to test this, and it seems to work for me.

Here's my Xaml. I've added a Reset Password button that clears the password in the code behind:
<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
	xmlns:sys="clr-namespace:System;assembly=mscorlib"
	xmlns:local="clr-namespace:Login"
	d:DesignWidth="1280" d:DesignHeight="720">

	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="200"/>
			<RowDefinition Height="100"/>
			<RowDefinition Height="100"/>
		</Grid.RowDefinitions>
		<PasswordBox x:Name="PasswordBox" Width="200" Height="30" FontSize="20"
					 local:PasswordHelper.Attach="True"
					 local:PasswordHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
		<Button Grid.Row="1" Width="170" Height="30" Content="Show Password" FontSize="20"
				Command="{Binding Show}"/>
		<Button Grid.Row="2" Width="170" Height="30" Content="Reset Password" FontSize="20"
				Command="{Binding ResetPassword}"/>
	</Grid>
</UserControl>
Here's my C++ PasswordHelper class. I've changed the names of a couple of functions, but it's basically the same code you've posted:
class PasswordHelper final: public Noesis::DependencyObject
{
public:
    static const char* GetPassword(DependencyObject* d);
    static void SetPassword(DependencyObject* d, const char* value);

    static bool GetAttach(DependencyObject* d);
    static void SetAttach(DependencyObject* d, bool value);

    static bool GetIsUpdating(DependencyObject* d);
    static void SetIsUpdating(DependencyObject* d, bool value);

public:
    static const Noesis::DependencyProperty* PasswordProperty;
    static const Noesis::DependencyProperty* AttachProperty;
    static const Noesis::DependencyProperty* IsUpdatingProperty;

private:
    static void OnPasswordChanged(DependencyObject* d,
        const Noesis::DependencyPropertyChangedEventArgs& e);
    static void OnAttachChanged(DependencyObject* d,
        const Noesis::DependencyPropertyChangedEventArgs& e);
    static void PasswordChanged(BaseComponent* d,
        const Noesis::RoutedEventArgs& e);

private:
    NS_DECLARE_REFLECTION(PasswordHelper, DependencyObject)
};

////////////////////////////////////////////////////////////////////////////////////////////////////
const char* PasswordHelper::GetPassword(DependencyObject* d)
{
    return d->GetValue<NsString>(PasswordProperty).c_str();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::SetPassword(DependencyObject* d, const char* value)
{
    d->SetValue<NsString>(PasswordProperty, value);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
bool PasswordHelper::GetAttach(DependencyObject* d)
{
    return d->GetValue<bool>(AttachProperty);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::SetAttach(DependencyObject* d, bool value)
{
    d->SetValue<bool>(AttachProperty, value);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
bool PasswordHelper::GetIsUpdating(DependencyObject* d)
{
    return d->GetValue<bool>(IsUpdatingProperty);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::SetIsUpdating(DependencyObject* d, bool value)
{
    d->SetValue<bool>(IsUpdatingProperty, value);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::OnPasswordChanged(DependencyObject* d,
        const DependencyPropertyChangedEventArgs& e)
{
    PasswordBox* passwordBox = NsDynamicCast<PasswordBox*>(d);
    if (!passwordBox)
        return;
    passwordBox->PasswordChanged() -= PasswordChanged;
    if (!GetIsUpdating(passwordBox))
    {
        passwordBox->SetPassword(((const NsString*)(e.newValue))->c_str());
    }
    passwordBox->PasswordChanged() += PasswordChanged;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::OnAttachChanged(DependencyObject* d,
        const DependencyPropertyChangedEventArgs& e)
{
    PasswordBox* passwordBox = NsDynamicCast<PasswordBox*>(d);
    if (!passwordBox)
        return;
    if (*((const bool*)(e.oldValue)))
    {
        passwordBox->PasswordChanged() -= PasswordChanged;
    }
    if (*((const bool*)(e.newValue)))
    {
        passwordBox->PasswordChanged() += PasswordChanged;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PasswordHelper::PasswordChanged(BaseComponent* d,
        const Noesis::RoutedEventArgs& e)
{
    PasswordBox* passwordBox = NsDynamicCast<PasswordBox*>(d);
    if (!passwordBox)
        return;
    SetIsUpdating(passwordBox, true);
    SetPassword(passwordBox, passwordBox->GetPassword());
    SetIsUpdating(passwordBox, false);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
NS_IMPLEMENT_REFLECTION(Login::PasswordHelper)
{
    NsMeta<TypeId>("Login.PasswordHelper");

    Ptr<UIElementData> data = NsMeta<UIElementData>(TypeOf<SelfClass>());
    data->RegisterProperty<NsString>(PasswordProperty, "Password",
        PropertyMetadata::Create(NsString(), &PasswordHelper::OnPasswordChanged));
    data->RegisterProperty<bool>(AttachProperty, "Attach",
        PropertyMetadata::Create(false, &PasswordHelper::OnAttachChanged));
    data->RegisterProperty<bool>(IsUpdatingProperty, "IsUpdating",
        PropertyMetadata::Create(false));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
const DependencyProperty* PasswordHelper::PasswordProperty;
const DependencyProperty* PasswordHelper::AttachProperty;
const DependencyProperty* PasswordHelper::IsUpdatingProperty;
And here's the code-behind in the blueprint:
Variables.PNG
Variables.PNG (7.55 KiB) Viewed 1539 times
Show.PNG
ResetPassword.PNG
ResetPassword.PNG (35.16 KiB) Viewed 1539 times
And here's everything in action:
Password.gif
 
ride_wind
Topic Author
Posts: 34
Joined: 07 Feb 2018, 03:33

Re: PasswordBox Set password value

08 Apr 2018, 12:11

Hi hcpizzi,

You set initial value of password in blueprint,
and then, test the passwordbox.

Very thanks.
 
User avatar
hcpizzi
Site Admin
Posts: 228
Joined: 09 Feb 2012, 12:40

Re: PasswordBox Set password value

08 Apr 2018, 12:48

I tried and it works too. Here's my init code:
Init.PNG
Here's it running:
InitialPassword.gif
Are you using Set w/ NotifyChanged to initialize the password? If you use a regular Set then it doesn't work:
BadInit.PNG
BadInit.PNG (27.31 KiB) Viewed 1526 times
BadInitialPassword.gif
 
ride_wind
Topic Author
Posts: 34
Joined: 07 Feb 2018, 03:33

Re: PasswordBox Set password value

13 Apr 2018, 11:19

Hi hcpizzi,

I tested it again and found that it seems error init in passwordbox.

I changed code like this.
<PasswordBox x:Name="PasswordBox" Width="200" Height="30" FontSize="20"
		 Foreground="White"
		 local:PasswordHelper.Attach="True"
		 local:PasswordHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="1" Width="170" Height="30" Content="Show Password" FontSize="20"
		Command="{Binding Show}"
		CommandParameter="{Binding ElementName=PasswordBox}"/>
Here is my init code and show code in Blueprint.
InitCode.png
ShowCode.png
I used blueptintFunctionLibrary to get password from passwordBox in C++ code.
FString UBPFunctionLibrary::GetPasswordFromPasswordBox(UObject* InPasswordBox)
{
	Noesis::Ptr<Noesis::BaseComponent> BaseComponent = NoesisCreateComponentForUObject(InPasswordBox);
	if (BaseComponent)
	{
		Noesis::PasswordBox* PasswordBox = NsDynamicCast<Noesis::PasswordBox*>(BaseComponent.GetPtr());
		if (PasswordBox)
		{
			return UTF8_TO_TCHAR(PasswordBox->GetPassword());
		}
	}
	return TEXT("");
}
Running Result.
Running.png
Very thanks.
 
User avatar
hcpizzi
Site Admin
Posts: 228
Joined: 09 Feb 2012, 12:40

Re: PasswordBox Set password value

23 Apr 2018, 17:14

Hi,

Sorry it took me so long to get back to this. I've tried your latest xaml, and it still works correctly for me. Here's the password initialization:
PW-Init.PNG
Here's the Show command:
PW-Show.PNG
Here's the result:
PW-Demo.gif

Who is online

Users browsing this forum: No registered users and 4 guests