Reputation: 27
I have a question. I search the problem but I didn't find a solution. Hope someone can help me.
I have a "DataGrid" and in this is a "DataGrid.RowDetailsTemplate". There are "Textbox" they show data from a list binding. And I want to change this data when I click on a button, but I cant read the Bindings so I cant change them. Hope someone know what I mean.
WPF code:
<dxn:NavBarGroup Name="_navBarOverlay" Header="Benutzerübersicht">
<Grid Style="{StaticResource GridStyleAccordion}">
<DataGrid x:Name="userDataGrid" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserResizeRows="True" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" AlternatingRowBackground="{DynamicResource WihaGrauB}" ColumnWidth="auto" ColumnHeaderHeight="30" AutoGenerateColumns="False" Grid.Row="0" Width="auto" Height="auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Benutzer" Binding="{Binding Name}" MinWidth="150" Width="2*" />
<DataGridTextColumn Header="OperatorID" Binding="{Binding OperatorID}" MinWidth="200" Width="3*"/>
<DataGridCheckBoxColumn Header="Aktiv" Binding="{Binding Aktiv}" MinWidth="50" Width="*"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DockPanel>
<Image DockPanel.Dock="Left" Source="{Binding ImageUrl}" Height="30" Width="25" Margin="10,0,0,0"/>
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Name: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="1" Margin="2,2,2,2"/>
<TextBox x:Name="nameText" Text="{Binding Path=Name, Mode=TwoWay}" Grid.Column="1" Width="150" TextAlignment="Center" HorizontalAlignment="Left" Grid.Row="1" Margin="2,2,2,2" />
<TextBlock Text="OperatorID: " FontFamily="{DynamicResource WihaFontFamaly}" Grid.Row="2" FontWeight="Bold" Margin="2,2,2,2"/>
<TextBox x:Name="operatorText" IsReadOnly="True" Text="{Binding OperatorID}" Width="150" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" TextAlignment="Center" Margin="2,2,2,2" />
<TextBlock Text="DeviceID: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="3" Margin="2,2,2,2"/>
<TextBox Text="{Binding DeviceID}" Grid.Column="3" Width="150" HorizontalAlignment="Left" Grid.Row="3" TextAlignment="Center" Margin="2,2,2,2"/>
<TextBlock Text="Passwort: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="4" Margin="2,2,2,2"/>
<TextBox Text="{Binding Passwort}" Grid.Column="4" Width="150" HorizontalAlignment="Left" Grid.Row="4" TextAlignment="Center" Margin="2,2,2,2"/>
<TextBlock Text="Aktiv:" FontWeight="Bold" FontFamily="{DynamicResource WihaFontFamaly}" Grid.Row="5" Margin="2,2,2,2"/>
<CheckBox Grid.Column="5" Grid.Row="5" IsChecked="{Binding Aktiv}" Margin="2,2,2,2" />
<Button Style="{StaticResource ButtonStyleWIHA}" Click="saveUserPanel_Click" Name="saveUserPanel" Content="Save" Grid.Row="6" />
</Grid>
</DockPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</dxn:NavBarGroup>
C# code where the data comes:
public void ShowAllOperator()
{
try
{
List<User> users = new List<User>();
// string sql = "SELECT * FROM wiha_Operators";
SqlConnection conn = new SqlConnection("Server=127.0.0.1;Database=Wiha;Trusted_Connection=true");
conn.Open();
// SqlCommand cmd = new SqlCommand(sql, conn);
SqlCommand cmd = new SqlCommand("dbo.wiha_operators_SelectAll", conn);
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
users.Add(new User() { Name = rdr.GetValue(2).ToString(), OperatorID = rdr.GetValue(0).ToString(), Aktiv = rdr.GetValue(4).Equals(true), ImageUrl = "C:/Users/Jason/Desktop/DeleteIcon.png", DeviceID = rdr.GetValue(1).ToString(), Passwort = rdr.GetValue(3).ToString() });
}
}
userDataGrid.ItemsSource = users;
}
catch (Exception)
{
throw;
}
}
Here I want to change them:
private void saveUserPanel_Click(object sender, RoutedEventArgs e)
{
}
My User list:
public class User
{
public string Name { get; set; }
public string OperatorID { get; set; }
public bool Aktiv { get; set; }
public string ImageUrl { get; set; }
public string DeviceID { get; set; }
public string Passwort { get; set; }
}
Hope you know what I want from you. You miss or didn't understand something? Ask me :)
EDIT
My new User class:
public class User : INotifyPropertyChanged
{
private string _name = string.Empty;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get { return this._name; }
set
{
if (value != this._name)
{
this._name = value;
NotifyPropertyChanged();
}
}
}
public string OperatorID { get; set; }
public bool Aktiv { get; set; }
public string ImageUrl { get; set; }
public string DeviceID { get; set; }
public string Passwort { get; set; }
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
I see only the old value.
Upvotes: 0
Views: 316
Reputation: 819
As @Ash suggests, you could use the DataContext
of the Button, which is a User
object, to access the Item, since it is part of the DataTemplate
private void saveUserPanel_Click(object sender, RoutedEventArgs e)
{
var user = (sender as Button).DataContext as User;
if(user != null)
{
user.Name = "NewName";
var active = user.Aktiv;
// ...
}
}
Notice that updating the user object in the code behind will not update your UI since User
does not implement INotifyPropertyChanged
.
Upvotes: 1