Using WPF, I have a ListBox
control with a DataTemplate
inside it. The relevant XAML code is shown below:
<ListBox Name="_todoList" Grid.Row="1" BorderThickness="2"
Drop="todoList_Drop" AllowDrop="True"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
AlternationCount="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Checked="CheckBox_Check" />
<TextBlock Name="descriptionBlock"
Grid.Column="1"
Text="{Binding Description}"
Cursor="Hand" FontSize="14"
ToolTip="{Binding Description}"
MouseDown="TextBlock_MouseDown" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
What I am trying to do is make the TextBlock
respond to a (double)click which turns it into a TextBox
. The user can then edit the description, and press return or change focus to make the change.
I have tried adding a TextBox
element in the same position as the TextBlock and making its visiblity Collapsed
, but I don't know how to navigate to the right TextBox
when the user has clicked on a TextBlock
. That is, I know the user has clicked on a certain TextBlock
, now which TextBox
do I show?
Any help would be appreciated greatly,
-Ko9
What I've done in these situations is used the XAML hierarchy to determine which element to show/hide. Something along the lines of:
<Grid>
<TextBlock MouseDown="txtblk_MouseDown" />
<TextBox LostFocus="txtbox_LostFocus" Visibility="Collapsed" />
</Grid>
with the code:
protected void txtblk_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBox txt = (TextBox)((Grid)((TextBlock)sender).Parent).Children[1];
txt.Visibility = Visibility.Visible;
((TextBlock)sender).Visibility = Visibility.Collapsed;
}
protected void txtbox_LostFocus(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)((Grid)((TextBox)sender).Parent).Children[0];
tb.Text = ((TextBox)sender).Text;
tb.Visibility = Visibility.Visible;
((TextBox)sender).Visibility = Visibility.Collapsed;
}
I always turn stuff like this that I'm going to reuse into a UserControl
, which I can add additional error handling to, and guarantee that the Grid
will only contain two items, and the order of them will never change.
EDIT: Additionally, turning this into a UserControl allows you to create a Text
property for each instantiation, so you can name each one and reference the text directly without fishing for the current value through the ((TextBox)myGrid.Children[1]).Text
casting. This will make your code much more efficient and clean. If you make it into a UserControl, you can also name the TextBlock
and TextBox
elements, so no casting is needed at all.
Refer to the Nathan Wheeler's code snippet, the following codes are complete UserControl source that I coded yesterday. Especially, Binding issues are addressed. Nathan's code is easy to follow, but needs some aid in order to work with databound text.
ClickToEditTextboxControl.xaml.cs
public partial class ClickToEditTextboxControl : UserControl
{
public ClickToEditTextboxControl()
{
InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(ClickToEditTextboxControl), new UIPropertyMetadata());
private void textBoxName_LostFocus(object sender, RoutedEventArgs e)
{
var txtBlock = (TextBlock)((Grid)((TextBox)sender).Parent).Children[0];
txtBlock.Visibility = Visibility.Visible;
((TextBox)sender).Visibility = Visibility.Collapsed;
}
private void textBlockName_MouseDown(object sender, MouseButtonEventArgs e)
{
var grid = ((Grid) ((TextBlock) sender).Parent);
var tbx = (TextBox)grid.Children[1];
((TextBlock)sender).Visibility = Visibility.Collapsed;
tbx.Visibility = Visibility.Visible;
this.Dispatcher.BeginInvoke((Action)(() => Keyboard.Focus(tbx)), DispatcherPriority.Render);
}
private void TextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e == null)
return;
if (e.Key == Key.Return)
{
textBoxName_LostFocus(sender, null);
}
}
}
ClickToEditTextboxControl.xaml
<UserControl x:Class="Template.ClickToEditTextboxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Name="root"
d:DesignHeight="30" d:DesignWidth="100">
<Grid>
<TextBlock Name="textBlockName" Text="{Binding ElementName=root, Path=Text}" VerticalAlignment="Center" MouseDown="textBlockName_MouseDown" />
<TextBox Name="textBoxName" Text="{Binding ElementName=root, Path=Text, UpdateSourceTrigger=PropertyChanged}" Visibility="Collapsed" LostFocus="textBoxName_LostFocus" KeyDown ="TextBoxKeyDown"/>
</Grid>
</UserControl>
And, finally, you can use this control in the XAML as below:
<Template1:ClickToEditTextboxControl Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="40" Height="23" />
Note that Mode=TwoWay, UpdateSourceTrigger=PropertyChanged is set. It enables to change the binded value in every type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With