EDIT: THIS CODE IS AN UPDATED VERSION USING THE RECOMMENDATIONS BELOW BUT STILL RESULTING IN THE SAME PROBLEM. LINK TO SAMPLE IMAGE OF PROBLEM, [EXAMPLE IMAGE].
This code is meant to update the "AddUserNewUsername" label on a tabitem for creating a new user account for my little program. It use to update the label once the user left the "First Name" and "Last Name" fields. However, after some testing, it looks like the "AddUserNewUsername" string IS being populated, but not updating from blank on the GUI.
I'm really confused. I'm new to binding but I don't really understand how this broke. The only thing I changed was the variable from "NewUsername" to "AddUserNewUsername" and updated everything along with it. Below is a snippet of the relevant code.
MainWindow XAML:
<Label Name="add_user_uname" Grid.Column="5" Grid.ColumnSpan="6" Grid.Row="7"
Content="{Binding Path=AddUserNewUsername, Mode=OneWay}"
Style="{DynamicResource add_user_label_uname}"/>
<TextBox Name="add_user_finame" Grid.Column="5" Grid.ColumnSpan="6" Grid.Row="2"
Text="{Binding Path=AddUserFirstName, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource add_user_text_box_std}"/>
<TextBox Name="add_user_lname" Grid.Column="5" Grid.ColumnSpan="6" Grid.Row="3"
Text="{Binding Path=AddUserLastName, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource add_user_text_box_std}"/>
MainWindow Code Behind:
public MainWindow()
{
InitializeComponent();
this.DataContext = id;
}
public Instance_Data id = new Instance_Data();
User_Management Class (Just to show where the message box comes from.):
public static void AttemptUserCreation(MainWindow parent, string finame, string lname, string email, string pword, string verify_pword, string uname, string current_user)
{
MessageBox.Show(parent.id.AddUserNewUsername);
return;
Instance_Data Class:
public class Instance_Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _addUserFirstName;
private string _addUserLastName;
public string AddUserFirstName
{
get { return _addUserFirstName; }
set
{
_addUserFirstName = value;
OnPropertyChanged("AddUserFirstName");
OnPropertyChanged("AddUserNewUsername");
}
}
public string AddUserLastName
{
get { return _addUserLastName; }
set
{
_addUserLastName = value;
OnPropertyChanged("AddUserLastName");
OnPropertyChanged("AddUserNewUsername");
}
}
public string AddUserNewUsername
{
get
{
if (!String.IsNullOrEmpty(AddUserFirstName))
{
return AddUserFirstName[0] + AddUserLastName;
}
else
{
return AddUserFirstName;
}
}
}
add_user_label_uname Style as requested:
<Style x:Key="add_user_label_uname" TargetType="{x:Type Label}">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Foreground" Value="#B45A00"/>
</Style>
I spent the end of my work day yesterday trying to figure this out and cannot seem to find my hiccup. Hopefully I provided enough data but should you need more just ask! Thank you!