I had copied your provided code and tried to run the application.It is showing me the exact as wanted.
1.One more thing for your information
CompID = (dgCompDtl.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
In your above line you are taking SelectedCells[2] for getting your CompID value which is actually at position no 1 as per your code in xaml so please replace it with
CompID = (dgCompDtl.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
and try to run the program . Might now your content will get displayed
the code working for me is as follows
Please take one Latest WPF project and then try below things
<Window x:Class="XYZ.Practice.Lesson1Chapter3Practice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Lesson1Chapter3Practice" Height="300" Width="300">
<Window.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontSize" Value="20" />
</Trigger>
</Style.Triggers>
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="White" />
<Setter Property="Width" Value="135" />
<Setter Property="Height" Value="30" />
</Style>
<Style TargetType="Label">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Width" Value="135" />
<Setter Property="Height" Value="33" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Width" Value="135" />
<Setter Property="Height" Value="30" />
</Style>
</Window.Resources>
<Grid>
<DataGrid Name="dgCompDtl" AlternatingRowremoved="#FFE2BFD0" Height="494" Width="1115" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="False" Loaded="dgCompDtl_Loaded" FontSize="12" SelectionChanged="dgCompDtl_SelectionChanged">
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="Mouse.MouseEnter" Handler="Show_PopupToolTip" />
<EventSetter Event="Mouse.MouseLeave" Handler="Hide_PopupToolTip"/>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Component_ID}" Width="50" Header="ID" >
<DataGridTextColumn.HeaderTemplate >
<DataTemplate>
<TextBlock Text="ID" TextWrapping="Wrap" FontWeight="Bold" FontSize="10"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Component_Desc}" Width="150" Header="Description" >
<DataGridTextColumn.HeaderTemplate >
<DataTemplate>
<TextBlock Text="Description" TextWrapping="Wrap" FontWeight="Bold" FontSize="10"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Per_Asset_Qty}" Width="75" Header="Per Asset Qty" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.HeaderTemplate >
<DataTemplate>
<TextBlock Text="Per Asset Qty" TextWrapping="Wrap" FontWeight="Bold" FontSize="10" HorizontalAlignment="Right"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.Background>
<SolidColorBrush />
</DataGrid.Background>
</DataGrid>
<Popup Height="Auto" Width="Auto" Name="MyToolTip" StaysOpen="True" AllowsTransparency="True" >
<Border BorderThickness="2" removed="BlanchedAlmond" >
<StackPanel Margin="30" Orientation="Horizontal">
<WrapPanel >
<Label Height="25" Content="Asset Component Description:" FontWeight="Bold" FontSize="12" Width="209"></Label>
<TextBox Height="26" Name="tbCompDesc1" TabIndex="1" Width="353" IsReadOnly="True" />
<TextBlock Text="
" Height="10"/>
<Label Content="Make and Model No.(Check if same for all)" Margin="50,0,0,0" FontWeight="Bold" FontSize="12"/>
<TextBox Height="26" Name="tbMkeMdl1" TabIndex="3" Width="259" Padding="0" MinLines="3" IsReadOnly="True" />
</WrapPanel>
<WrapPanel>
<Label Height="25" Width="209" Content="Component Qty for a Single Asset:" FontWeight="Bold" FontSize="12"></Label>
<TextBox Height="26" Name="tbCompQty1" TabIndex="3" Width="95" Padding="0" MinLines="4" IsReadOnly="True" />
<Label Height="25" Width="207" Content="Component Value for Single Asset:" FontWeight="Bold" FontSize="12" Margin="50,0,0,0"></Label>
<TextBox Height="26" Name="tbCompAmt1" TabIndex="3" Width="173" MinLines="5" TextAlignment="Right" IsReadOnly="True" />
<TextBlock Text="
" Height="10"/>
</WrapPanel>
<WrapPanel>
<Label Content="Depreciation Type:" FontWeight="Bold" Height="26" FontSize="12" Width="209" />
<ComboBox FontSize="16" Height="26" Name="cmbDeprtype1" Width="299" TabIndex="6" IsReadOnly="True" />
<Label Height="25" Width="118" Content=" Depreciation Amt:" FontWeight="Bold" FontSize="12" Margin="50,0,0,0"></Label>
<TextBox Height="26" Name="tbDAmt1" TabIndex="7" Width="135" TextAlignment="Right" IsReadOnly="True" />
<Label Height="25" Width="119" Content=" Depreciation Rate:" FontWeight="Bold" FontSize="12" Margin="50,0,0,0"></Label>
<TextBox Height="26" Name="tbDRate1" TabIndex="8" Width="94" TextAlignment="Right" IsReadOnly="True" />
</WrapPanel>
</StackPanel>
</Border>
</Popup>
</Grid>
</Window>
///xaml.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace XYZ.Practice
{
public partial class Lesson1Chapter3Practice : Window
{
DataTable DTComp = new DataTable();
public Lesson1Chapter3Practice()
{
InitializeComponent();
DTComp.Columns.Add("Component_ID");
DTComp.Columns.Add("Component_Desc");
DTComp.Columns.Add("Per_Asset_Qty");
DTComp.Columns.Add("Amt");
DTComp.Columns.Add("Rate");
DTComp.Columns.Add("DeprAmt");
DTComp.Columns.Add("Make_Model_No");
DTComp.Rows.Add("1", "Component_Desc", "Per_Asset_Qty", "Amt", "Rate", "Make_Model_No");
DTComp.AcceptChanges();
dgCompDtl.ItemsSource = DTComp.DefaultView;
dgCompDtl.SelectedIndex = 0;
}
private void Show_PopupToolTip(object sender, MouseEventArgs e)
{
object item = dgCompDtl.SelectedItem;
string CompID = (dgCompDtl.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
foreach (DataRow Dr in DTComp.Rows)
{
if (Dr["Component_ID"].ToString() == CompID)
{
tbCompDesc1.Text = Dr["Component_Desc"].ToString();
tbCompQty1.Text = Dr["Per_Asset_Qty"].ToString();
tbCompAmt1.Text = Dr["Amt"].ToString();
tbDRate1.Text = Dr["Rate"].ToString();
tbDAmt1.Text = Dr["DeprAmt"].ToString();
tbMkeMdl1.Text = Dr["Make_Model_No"].ToString();
}
}
MyToolTip.Placement = PlacementMode.MousePoint;
MyToolTip.IsOpen = true;
}
private void Hide_PopupToolTip(object sender, MouseEventArgs e)
{
MyToolTip.IsOpen = false;
}
private void dgCompDtl_Loaded(object sender, RoutedEventArgs e)
{
}
private void dgCompDtl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
during your code copy i came some error related to xaml like below
removed="BlanchedAlmond"
AlternatingRowremoved="#FFE2BFD0"
I had changed them respectively.