I've got a small WPF form with a Listview (bound to an observablecolletion)
<ListView Name="Employees_Listview" ItemsSource="{Binding Employees, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding YourSelectedItem, Mode=TwoWay}" Height="86" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
2 textboxes
<TextBox Name="FirstName" Width="136" Text="{Binding SelectedItem.FirstName, ElementName=Employees_Listview, Mode=TwoWay}" />
<TextBox Name="LastName" Width="136" Text="{Binding SelectedItem.LastName, ElementName=Employees_Listview, Mode=TwoWay}" />
and a button
<Button Command="{Binding UpdateEmployeeCommand}" Margin="0,0,10,0">Update</Button>
Both textboxes are bound to the selected item in listview so when I update a textbox value it will automatically reflect the changes in the listview. Now I would like to update the database first and if there is no error I would like to update the listview. My update code looks like this
var container = ContainerConfig.Configure();
using (var scope = container.BeginLifetimeScope())
{
var SelectedEmployee = scope.Resolve<IEmployeeRepository>();
if (!SelectedEmployee.Update(YourSelectedItem))
{
MessageBox.Show("Datensatz konnte nicht aktualisiert werden!" + "\n" + "Bitte den Administrator verständigen!");
return;
};
}
I tried to set the textbox mode to OneWay but due the fact it's bound to the SelectedItem I can't get the new textbox value, only the "old" listview value. How can I check and update my database first before refreshing the listview?
UPDATE Maybe I have to choose another approach like this: TextBox bound a new property declared
View
<TextBox Text="{Binding NewFirstName}" Width="123"></TextBox>
and my ViewModel
//TEST
private string newfirstname;
public string NewFirstName
{
get { return newfirstname; }
set
{
newfirstname= value;
RaisePropertyChanged("NewFirstName");
}
}
//TEST
public EmployeeEntity YourSelectedItem
{
get
{
return _yourSelectedItem;
}
set
{
NewFirstName = value.FirstName;
_yourSelectedItem = value;
RaisePropertyChanged("YourSelectedItem");
}
}
Guess this is not the best way to go :/
ListViewupdates but theTextBoxvalues do not? - Chris Mack