Reputation: 103
I'm trying to get the Label.Content inside a ListBox.DataTemplate as described in title, my problem is that in all the ways I've tried all gave error, below is my code.
xaml:
<ListBox x:Name="UsersList" Margin="644,50,50,31.999" ItemsSource="{Binding}" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="50" Width="654" MouseEnter="SetHashUser">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label x:Name="HashUser" Content="{Binding Path=USER_HASH}" Visibility="Collapsed"/>
<TextBlock x:Uid="RealAcessoUserDisplay" Text="{Binding Path=NOME}" Grid.RowSpan="1" Grid.Row="0" Background="#FFF0F0F0"/>
<TextBlock x:Uid="RealAcessoUserDisplay" Text="{Binding Path=NIVEL_ACESSO}" Grid.RowSpan="1" Grid.Row="1" Background="#FFF0F0F0"/>
<Menu Grid.RowSpan="1" Grid.Row="2">
<MenuItem Header="Ativar"/>
<MenuItem Header="Desativar"/>
<MenuItem Header="Detalhes" Click="ShowUser"/>
<MenuItem Header="Editar"/>
</Menu>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
DB Connection C#
private void Listtks()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["BellaContext"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT USER_HASH,NOME,NIVEL_ACESSO FROM USUARIOS";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
UsersList.ItemsSource = dt.DefaultView;
}
My goal is when clicking on an item in the list, the SelectedRow gets the User_Hash.Content.
Event:
private void SetHashUser(object sender, MouseEventArgs e)
{
display.Content = UsersList.SelectedItem(HashUser.Content);
}
Error:
Error:
error CS0103: The name "HashUser" does not exist in the current context
error CS1955: The non-callable member "Selector.SelectedItem" can not be used as
a method.
Upvotes: 0
Views: 148
Reputation: 5366
ListBox SelectedItem is not a method. It is property which gives the selecteditem of the listbox. In this case the selecteditem will be DataRow since you bind DataTable to ListBox.
Assuming USER_HASH as string. Try below code.
private void SetHashUser(object sender, MouseEventArgs e)
{
var dataRow = UsersList.SelectedItem;
display.Content = dataRow.Field<string>("USER_HASH ");
}
Upvotes: 1