Referring to a TextBox in a cell in grid mode
So what I did ListView
with GridView
in my prop view:
<ListView Grid.Row="1" Name="TicketListView" Margin="20,5,20,5">
<ListView.View>
<GridView>
<GridViewColumn Header="Owned" DisplayMemberBinding="{Binding Owned}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price, ConverterCulture='en-US', StringFormat={}{0:C2}}"/>
<GridViewColumn Header="Amount">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="20" TextAlignment="Center" Margin="5" Text="{Binding Bought, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
...
</ListView>
I want to focus on specific TextBox
when selecting an item in this list view, so you don't have to click on TextBox
to write, but instead you can click on the item (row) and start changing its value. (I hope you understand;()
Is this possible, or should I be doing this list in a different way?
source to share
You can handle the event PreviewMouseLeftButtonDown
for ListViewItem
and get a reference to TextBox
using the class VisualTreeHelper
:
<ListView Grid.Row="1" Name="TicketListView">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="TicketListView_PreviewMouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Owned" DisplayMemberBinding="{Binding Owned}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price, ConverterCulture='en-US', StringFormat={}{0:C2}}"/>
<GridViewColumn Header="Amount">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="20" TextAlignment="Center" Margin="5" Text="{Binding Bought, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
private void TicketListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListViewItem lvi = sender as ListViewItem;
TextBox textBox = GetChildOfType<TextBox>(lvi);
if (textBox != null)
{
textBox.Dispatcher.BeginInvoke(new Action(() =>
{
bool b = textBox.Focus();
Keyboard.Focus(textBox);
}));
}
}
private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
source to share