Recently I noticed that my TextBoxes aren't updating anymore with TwoWay Bindings. So if i'm changing the Property it doesn't update the text in the Textbox.
XAML:
<Window
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"
x:Name="frm_Address1" x:Class="MegaCAD_Verwaltung.Address.frm_Address"
mc:Ignorable="d"
Title="Address" Height="721" Width="1235"
Closing="Frm_Address_OnClosing"
ContentRendered="Frm_Address_OnContentRendered">
<Grid>
<TabControl x:Name="tc_adress" Margin="10">
<TabItem x:Name="ti_New" Header="Neu" Height="30px">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60*"/>
<ColumnDefinition Width="1141*"/>
</Grid.ColumnDefinitions>
<Label Content="Kunden ID" HorizontalAlignment="Left" Margin="12,16,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<TextBox x:Name="inp_CustomerId" HorizontalAlignment="Left" Margin="44,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="23" Width="200" Text="{Binding NewAddress.CustomerId, Mode=TwoWay}" Grid.Column="1"/>
<Label Content="Firma" HorizontalAlignment="Left" Margin="12,53,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="inp_name_Copy" HorizontalAlignment="Left" Height="23" Margin="44,56,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="201" Grid.Column="1" Text="{Binding NewAddress.CompanyName, Mode=TwoWay}"/>
<Label Content="Kontaktperson" HorizontalAlignment="Left" Margin="12,81,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<TextBox x:Name="inp_name" HorizontalAlignment="Left" Height="23" Margin="44,84,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="201" Grid.Column="1" Text="{Binding NewAddress.ContactPerson, Mode=TwoWay}"/>
<Label Content="Addresszeile 2" HorizontalAlignment="Left" Margin="13,109,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" />
<TextBox x:Name="inp_name_Copy1" HorizontalAlignment="Left" Height="23" Margin="44,112,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="201" Text="{Binding NewAddress.AddressLineTwo, Mode=TwoWay}" Grid.Column="1"/>
<Label Content="Strasse / Nr" HorizontalAlignment="Left" Margin="12,136,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<TextBox x:Name="inp_Street" HorizontalAlignment="Left" Height="23" Margin="44,140,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="145" Grid.Column="1" Text="{Binding NewAddress.StreetName, Mode=TwoWay}"/>
<Label Content="Land" HorizontalAlignment="Left" Margin="12,192,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="inp_StreetNr" HorizontalAlignment="Left" Height="23" Margin="197,140,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="48" Grid.Column="1" Text="{Binding NewAddress.HouseNr, Mode=TwoWay}"/>
<Label Content="PLZ / Ort" HorizontalAlignment="Left" Margin="12,164,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<TextBox x:Name="inp_Plz" HorizontalAlignment="Left" Height="23" Margin="44,168,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="55" Grid.Column="1" Text="{Binding NewAddress.PlzNumber, Mode=TwoWay}"/>
<TextBox x:Name="inp_City" HorizontalAlignment="Left" Height="23" Margin="103,168,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="142" Grid.Column="1" Text="{Binding NewAddress.LocationName, Mode=TwoWay}"/>
<Button x:Name="btn_NewSave" Content="Button" Margin="0,10,8,0" VerticalAlignment="Top" Style="{DynamicResource SaveButton}" Height="32" HorizontalAlignment="Right" Width="32" Grid.Column="1" Command="{Binding NewAddress.SaveNewAddressCommand}"/>
<DataGrid HorizontalAlignment="Left" Margin="10,266,0,10" Width="781" Grid.ColumnSpan="2" AutoGenerateColumns="False" RowHeaderWidth="0" ItemsSource="{Binding NewAddress.LicensesOwned, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Visibility="Hidden" Binding="{Binding id}"/>
<DataGridTextColumn Header="Anzahl" Width="0.3*" Binding="{Binding amount}"/>
<DataGridComboBoxColumn Header="Produkt" Width="1*" ItemsSource="{Binding Source={StaticResource AllProductsSource}}" SelectedValuePath="id" SelectedValueBinding="{Binding product_id}" DisplayMemberPath="name"/>
<DataGridTemplateColumn Header="WV-Ablauf" SortMemberPath="MaintenanceExpire" Width="0.7*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker IsEnabled="{Binding isActive}" SelectedDate="{Binding MaintenanceExpire, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Benutzer" Width="0.7*" Binding="{Binding user}"/>
<DataGridTextColumn Header="Kommentar" Width="1*" Binding="{Binding comment}"/>
<DataGridTextColumn Header="Dongle ID" Width="1*" Binding="{Binding dongle_id}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dg_Mail" HorizontalAlignment="Left" Height="178" Margin="736,47,0,0" VerticalAlignment="Top" Width="395" Grid.Column="1" RowHeaderWidth="0" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False" ItemsSource="{Binding NewAddress.ContactsOwned}">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Visibility="Hidden"/>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding ContactName}"/>
<DataGridTextColumn Header="Telefon" Width="*" Binding="{Binding PhoneNumber}"/>
<DataGridTextColumn Header="E-Mail" Width="*" Binding="{Binding EmailAddress}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dg_links" HorizontalAlignment="Left" Height="173" Margin="736,449,0,0" VerticalAlignment="Top" Width="395" RowHeaderWidth="0" Grid.Column="1" AutoGenerateColumns="False" ItemsSource="{Binding NewAddress.LinksOwned}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Visibility="Hidden"/>
<DataGridHyperlinkColumn Binding="{Binding Link}" Width="196" Header="Link">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="OnOpenLinkInBrowser"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
<DataGridTextColumn Binding="{Binding Comment}" Width="196" Header="Bemerkung"/>
</DataGrid.Columns>
</DataGrid>
<Label Content="Kontakte" HorizontalAlignment="Left" Margin="736,18,0,0" VerticalAlignment="Top" Grid.Column="1"/>
<TextBox x:Name="tb_Description" HorizontalAlignment="Left" Height="178" Margin="736,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="395" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Grid.Column="1" Text="{Binding NewAddress.Comment}"/>
<Label Content="Bemerkungen" HorizontalAlignment="Left" Margin="736,237,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.488,1.577" Grid.Column="1"/>
<ComboBox x:Name="cb_CountryCode" HorizontalAlignment="Left" Margin="44,196,0,0" VerticalAlignment="Top" Width="54" Grid.Column="1" ItemsSource="{Binding NewAddress.AvailableCodes, Mode=TwoWay}" DisplayMemberPath="Code" SelectedValuePath="Code" SelectedItem="{Binding NewAddress.CCode}" SelectedValue="{Binding NewAddress.CCode.Code}"/>
<DataGrid x:Name="dg_Branche" HorizontalAlignment="Left" CanUserResizeColumns="False" RowHeaderWidth="0" Height="208" Margin="523,16.05,0,0" VerticalAlignment="Top" Width="189" UseLayoutRounding="False" AutoGenerateColumns="False" ItemsSource="{Binding NewAddress.IndustrySectorsOwned, Mode=TwoWay}" Grid.Column="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding IndustrySectorId}" Visibility="Hidden"/>
<DataGridTextColumn Binding="{Binding Name}" Width="2*" Header="Branche" IsReadOnly="True"/>
<DataGridCheckBoxColumn Binding="{Binding IsActive}" Width="0.75*" Header="Aktiv" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dg_Branche_Copy" HorizontalAlignment="Left" CanUserResizeColumns="False" RowHeaderWidth="0" Height="208" Margin="289.5,16.05,0,0" VerticalAlignment="Top" Width="190" UseLayoutRounding="False" AutoGenerateColumns="False" ItemsSource="{Binding NewAddress.AddressTypesOwned, Mode=TwoWay}" Grid.Column="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding AddressTypeId}" Visibility="Hidden"/>
<DataGridTextColumn Binding="{Binding Name}" Width="2*" Header="Adresstyp" IsReadOnly="True"/>
<DataGridCheckBoxColumn Binding="{Binding IsActive}" Width="0.75*" Header="Aktiv" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
C#:
namespace MegaCAD_Verwaltung.Address
{
using Data.Models;
/// <summary>
/// Interaktionslogik für frm_Address.xaml
/// </summary>
public partial class frm_Address : Window
{
private CancellationTokenSource DataGridFillerKiller = new CancellationTokenSource();
private CancellationTokenSource DataGridSearchKiller = new CancellationTokenSource();
public Task DataGridFiller { get; set; }
public Task DataGridSearch { get; set; }
public NewAddressTab NewAddress { get; set; }
public ObservableCollection<AddressForSearchGrid> AllAddresses
{
get { return _AllAddresses; }
set
{
_AllAddresses = value;
OnPropertyChanged("AllAddresses");
}
}
public ObservableCollection<AddressTypeForSearch> AllAddressTypes
{
get { return _allAddressTypes; }
set
{
_allAddressTypes = value;
OnPropertyChanged("AllAddressTypes");
}
}
public ObservableCollection<IndustrySectorForSearch> AllIndustrySectors
{
get { return _allIndustrySectors; }
set
{
_allIndustrySectors = value;
OnPropertyChanged("AllIndustrySectors");
}
}
public AddressTypeForSearch SelectedAddressType { get; set; } = new AddressTypeForSearch();
public IndustrySectorForSearch SelectedIndustrySector { get; set; } = new IndustrySectorForSearch();
public string SearchId { get; set; }
public string SearchName { get; set; }
public string SearchLocation { get; set; }
public string SearchPostal { get; set; }
public string SearchEmail { get; set; }
private ObservableCollection<AddressTypeForSearch> _allAddressTypes = new ObservableCollection<AddressTypeForSearch>();
private ObservableCollection<IndustrySectorForSearch> _allIndustrySectors = new ObservableCollection<IndustrySectorForSearch>();
public ICommand LoadAddressesCommand { get; set; }
public ICommand ExecuteSearchCommand { get; set; }
private object _AllAddressLock { get; set; } = new object();
private ObservableCollection<AddressForSearchGrid> _AllAddresses = new ObservableCollection<AddressForSearchGrid>();
public class NewAddressTab
{
public Address AddressToEdit { get; set; }
public CountryCodeEntry CCode { get; set; }
private string _CustomerId;
public string CustomerId
{
get { return _CustomerId; }
set
{
_CustomerId = value;
OnPropertyChanged("CustomerId");
}
}
public string CompanyName { get; set; }
public string ContactPerson { get; set; }
public string AddressLineTwo { get; set; }
public string StreetName { get; set; }
public string HouseNr { get; set; }
public string PlzNumber { get; set; }
public string LocationName { get; set; }
public string Comment { get; set; }
public List<CountryCodeEntry> AvailableCodes { get; set; } = new List<CountryCodeEntry>();
public ObservableCollection<AddressTypeGrid> AddressTypesOwned { get; set; } =
new ObservableCollection<AddressTypeGrid>();
public ObservableCollection<IndustrySectorGrid> IndustrySectorsOwned { get; set; } =
new ObservableCollection<IndustrySectorGrid>();
public ObservableCollection<LinksGridEntry> LinksOwned { get; set; } =
new ObservableCollection<LinksGridEntry>();
public ObservableCollection<LicenseGridEntry> LicensesOwned { get; set; } =
new ObservableCollection<LicenseGridEntry>();
public ObservableCollection<Product> AllProducts { get; set; } = new ObservableCollection<Product>();
public ObservableCollection<ContactGridEntry> ContactsOwned { get; set; } =
new ObservableCollection<ContactGridEntry>();
public LinksGridEntry LinkGridSelectedItem { get; set; }
public ICommand SaveNewAddressCommand { get; set; }
private frm_Address Parent;
public NewAddressTab(frm_Address parent)
{
SaveNewAddressCommand = new ActionCommand(p => SaveView());
AddressToEdit = new Address();
Parent = parent;
FillFields();
}
public void FillFields()
{
GetProducts();
GetCountryCodes();
GetAddressTypes();
GetIndustrySectors();
}
private void GetCountryCodes()
{
// Get Country Codes for the Combobox
List<CountryCode> ccModels = CountryCode.All();
foreach (var model in ccModels)
{
AvailableCodes.Add(new CountryCodeEntry(new CountryCode(model.id)));
}
}
private void GetAddressTypes()
{
//Get the addresstypes assigned to the Address
List<AddressType> allAddressTypes = AddressType.All();
foreach (AddressType addresstype in allAddressTypes)
{
AddressTypesOwned.Add(new AddressTypeGrid(addresstype.id, false));
}
}
private void GetIndustrySectors()
{
//Get the industry_sectors assigned to the Address
List<IndustrySector> allIndustrySectors = IndustrySector.All();
foreach (IndustrySector industrySector in allIndustrySectors)
{
IndustrySectorsOwned.Add(new IndustrySectorGrid(industrySector.id, false));
}
}
private void GetProducts()
{
foreach (Product product in Product.All())
{
AllProducts.Add(product);
}
}
private void SaveView()
{
//Setzt die Werte des Addressobjekts um dieses zu speichern
AddressToEdit.customerID = new Unique<string>(CustomerId);
AddressToEdit.company_name = new NullableString(CompanyName);
AddressToEdit.contact_name = new NullableString(ContactPerson);
AddressToEdit.addressline_2 = new NullableString(AddressLineTwo);
AddressToEdit.street = new NullableString(StreetName);
AddressToEdit.house_nr = new NullableString(HouseNr);
AddressToEdit.postal_code = new NullableString(PlzNumber);
AddressToEdit.location = new NullableString(LocationName);
AddressToEdit.comment = new NullableString(Comment);
AddressToEdit.updated_by = Properties.Settings.Default.user;
if (CCode != null)
{
AddressToEdit.fk_country_code =
new ForeignKey(new CountryCode(), ForeignKey.KeyType.NO_ACTION, true, CCode.Id);
}
else
{
AddressToEdit.fk_country_code = new ForeignKey(new CountryCode(), ForeignKey.KeyType.NO_ACTION, true, 0);
}
AddressToEdit.Save();
SaveAddressTypes();
SaveIndustrySectors();
SaveLinks();
SaveLicenses();
SaveContacts();
MessageBox.Show("Adresse wurde gespeichert!", "Erfolg!", MessageBoxButton.OK,
MessageBoxImage.Information);
ClearFields();
string displayName = "";
if (AddressToEdit.company_name.Value != "")
{
displayName = AddressToEdit.company_name.Value;
}
else
{
displayName = AddressToEdit.contact_name.Value;
}
Parent.NewTab(AddressToEdit.id, displayName);
}
private void ClearFields()
{
//Clear all the Fields
CCode = null;
CustomerId = null;
CompanyName = null;
ContactPerson = null;
AddressLineTwo = null;
StreetName = null;
HouseNr = null;
PlzNumber = null;
LocationName = null;
Comment = null;
LinksOwned.Clear();
LinkGridSelectedItem = null;
AddressTypesOwned.Clear();
IndustrySectorsOwned.Clear();
AvailableCodes.Clear();
LicensesOwned.Clear();
ContactsOwned.Clear();
LinkGridSelectedItem = null;
}
private void SaveContacts()
{
foreach (ContactGridEntry contactGridEntry in ContactsOwned)
{
new Contact()
{
contactname = new NullableString((contactGridEntry.ContactName).ToString()),
email = new NullableString((contactGridEntry.EmailAddress).ToString()),
phonenumber = new NullableString((contactGridEntry.PhoneNumber).ToString()),
fk_address_id = new ForeignKey(new Contact(), ForeignKey.KeyType.CASCADE, false, AddressToEdit.id),
updated_by = Properties.Settings.Default.user,
}.Save();
}
}
private void SaveLinks()
{
foreach (LinksGridEntry linksGridEntry in LinksOwned)
{
new Link()
{
link_string = linksGridEntry.Link,
comment = new NullableString(linksGridEntry.Comment),
fk_address_id = new ForeignKey(new Address(), ForeignKey.KeyType.CASCADE, false, AddressToEdit.id),
updated_by = linksGridEntry.Updated_by,
}.Save();
}
}
private void SaveLicenses()
{
foreach (LicenseGridEntry licenseGridEntry in LicensesOwned)
{
new License()
{
fk_address_id = new ForeignKey(new Address(), ForeignKey.KeyType.CASCADE, false, AddressToEdit.id),
fk_product_id = new ForeignKey(new Product(), ForeignKey.KeyType.SET_NULL, true, licenseGridEntry.product_id),
amount = licenseGridEntry.amount,
maintenace_Expire = licenseGridEntry.maintenance_expire,
user = new NullableString(licenseGridEntry.user),
comment = new NullableString(licenseGridEntry.comment),
dongle_id = new NullableString(licenseGridEntry.dongle_id),
updated_by = Properties.Settings.Default.user,
}.Save();
}
}
public void SaveAddressTypes()
{
foreach (AddressTypeGrid addressTypeGrid in AddressTypesOwned)
{
List<Address_AddressType> allAddressAddressTypes = Address_AddressType.WhereAnd(
new[] { "fk_address_id", "fk_addresstype_id" },
new object[] { AddressToEdit.id, addressTypeGrid.AddressTypeId });
if (allAddressAddressTypes.Count == 0 && addressTypeGrid.IsActive)
{
new Address_AddressType()
{
fk_address_id = new ForeignKey(new Address(), ForeignKey.KeyType.CASCADE, false, AddressToEdit.id),
fk_addresstype_id = new ForeignKey(new AddressType(), ForeignKey.KeyType.CASCADE, false, addressTypeGrid.AddressTypeId)
}.Save();
}
else if (allAddressAddressTypes.Count != 0 && !addressTypeGrid.IsActive)
{
allAddressAddressTypes[0].Delete();
}
}
}
private void SaveIndustrySectors()
{
foreach (IndustrySectorGrid industrySectorGrid in IndustrySectorsOwned)
{
List<Address_IndustrySector> allAddressIndustrySectors = Address_IndustrySector.WhereAnd(
new[] { "fk_address_id", "fk_industry_sector_id" },
new object[] { AddressToEdit.id, industrySectorGrid.IndustrySectorId });
if (allAddressIndustrySectors.Count == 0 && industrySectorGrid.IsActive)
{
new Address_IndustrySector()
{
fk_address_id = new ForeignKey(new Address(), ForeignKey.KeyType.CASCADE, false, AddressToEdit.id),
fk_industry_sector_id = new ForeignKey(new IndustrySector(), ForeignKey.KeyType.CASCADE, false, industrySectorGrid.IndustrySectorId)
}.Save();
}
else if (allAddressIndustrySectors.Count != 0 && !industrySectorGrid.IsActive)
{
allAddressIndustrySectors[0].Delete();
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string proprtyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(proprtyName));
}
#endregion
}
private readonly ObservableCollection<ITab> tabs;
....
AFAIK i specified that it's a TwoWay binding at every TextBox. I also implemented the INotifyPropertyChanged event for the Properties / TextBoxes that where not updating.
Am I missing something?
Thanks for the help
EDIT: specified problem.