PasswordBox Set password value
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.
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.
Re: PasswordBox Set password value
Hi, could you please paste the XAML here?
Thank you.
Thank you.
Re: PasswordBox Set password value
Hi, could you please paste the XAML here?
Thank you.
Code: Select all
<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>
I want to bind Password Property in passwordbox using AttachProperty,
But I found the PasswordBox showing length Is random.
Xaml code
Code: Select all
<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>
Code: Select all
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);
}
}
Detail to see image. sorry for i can't uploade the gif image, because it is to bigger.
Thanks.
Re: PasswordBox Set password value
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:
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:
And here's the code-behind in the blueprint:
And here's everything in action:
Here's my Xaml. I've added a Reset Password button that clears the password in the code behind:
Code: Select all
<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>
Code: Select all
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;
Re: PasswordBox Set password value
Hi hcpizzi,
You set initial value of password in blueprint,
and then, test the passwordbox.
Very thanks.
You set initial value of password in blueprint,
and then, test the passwordbox.
Very thanks.
Re: PasswordBox Set password value
I tried and it works too. Here's my init code:
Here's it running:
Are you using Set w/ NotifyChanged to initialize the password? If you use a regular Set then it doesn't work:
Here's it running:
Are you using Set w/ NotifyChanged to initialize the password? If you use a regular Set then it doesn't work:
Re: PasswordBox Set password value
Hi hcpizzi,
I tested it again and found that it seems error init in passwordbox.
I changed code like this.
Here is my init code and show code in Blueprint.
I used blueptintFunctionLibrary to get password from passwordBox in C++ code.
Running Result.
Very thanks.
I tested it again and found that it seems error init in passwordbox.
I changed code like this.
Code: Select all
<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}"/>
Code: Select all
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("");
}
Re: PasswordBox Set password value
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:
Here's the Show command:
Here's the result:
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:
Here's the Show command:
Here's the result:
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 25 guests