Design your elements like below:
<Window x:Class="FirstWPFApplication.DridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DridDemo" Height="150" Width="300">
<Grid ShowGridLines="False" Name="MyGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Col0" Width="*" />
<ColumnDefinition Name="Col1" Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Name="Row0" Height="Auto" />
<RowDefinition Name="Row1" Height="Auto" />
<RowDefinition Name="Row2" Height="Auto" />
</Grid.RowDefinitions>
<Label Name="lblName" Grid.Row="0" Grid.Column="0" Margin="5,5,5,5">
Name:
</Label>
<DockPanel Name="NameDocP" Grid.Row="0" Grid.Column="1" >
<TextBox Name="txtName" Margin="5,5,5,5" GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
[Sample textbox]
</TextBox>
</DockPanel>
<Label Name="lblPassword" Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" >
Password:
</Label>
<DockPanel Name="PwdDocP" Grid.Row="1" Grid.Column="1" >
<TextBox Name="txtPwd" Margin="5,5,5,5"
GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
[Sample textbox]
</TextBox>
</DockPanel>
<Button Name="cmdCancel" Width="45" Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Left" Margin="10,10,0,0" >
Cancel
</Button>
<Button Name="cmdOK" Width="45" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" Margin="0,10,10,0" >
OK
</Button>
</Grid>
</Window>
In the code behing you can you some thing like below to change the color of the cell:
private void txtPwd_GotFocus(object sender, RoutedEventArgs e)
{
if (sender == txtName)
{
NameDocP.Background = Brushes.Yellow;
PwdDocP.Background = Brushes.White;
}
else
{
NameDocP.Background = Brushes.White;
PwdDocP.Background = Brushes.Yellow;
}
}
private void txtName_LostFocus(object sender, RoutedEventArgs e)
{
if (sender == txtName)
{
NameDocP.Background = Brushes.White;
}
else
{
PwdDocP.Background = Brushes.White;
}
}
<Window x:Class="FirstWPFApplication.DridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DridDemo" Height="150" Width="300">
<Grid ShowGridLines="False" Name="MyGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Col0" Width="*" />
<ColumnDefinition Name="Col1" Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Name="Row0" Height="Auto" />
<RowDefinition Name="Row1" Height="Auto" />
<RowDefinition Name="Row2" Height="Auto" />
</Grid.RowDefinitions>
<Label Name="lblName" Grid.Row="0" Grid.Column="0" Margin="5,5,5,5">
Name:
</Label>
<DockPanel Name="NameDocP" Grid.Row="0" Grid.Column="1" >
<TextBox Name="txtName" Margin="5,5,5,5" GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
[Sample textbox]
</TextBox>
</DockPanel>
<Label Name="lblPassword" Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" >
Password:
</Label>
<DockPanel Name="PwdDocP" Grid.Row="1" Grid.Column="1" >
<TextBox Name="txtPwd" Margin="5,5,5,5"
GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
[Sample textbox]
</TextBox>
</DockPanel>
<Button Name="cmdCancel" Width="45" Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Left" Margin="10,10,0,0" >
Cancel
</Button>
<Button Name="cmdOK" Width="45" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" Margin="0,10,10,0" >
OK
</Button>
</Grid>
</Window>
In the code behing you can you some thing like below to change the color of the cell:
private void txtPwd_GotFocus(object sender, RoutedEventArgs e)
{
if (sender == txtName)
{
NameDocP.Background = Brushes.Yellow;
PwdDocP.Background = Brushes.White;
}
else
{
NameDocP.Background = Brushes.White;
PwdDocP.Background = Brushes.Yellow;
}
}
private void txtName_LostFocus(object sender, RoutedEventArgs e)
{
if (sender == txtName)
{
NameDocP.Background = Brushes.White;
}
else
{
PwdDocP.Background = Brushes.White;
}
}