0

I'm creating a simple file manager. I try to open a new directory, by double-click on ListBox. How I can get secondtb1.Text property value in my code using MouseDoubleClick?

My XAML

<ListBox x:Name="secondPageListbox" Background="{x:Null}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <Grid>
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="320"/>
               <ColumnDefinition Width="50"/>
               <ColumnDefinition Width="186"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
               <TextBlock x:Name="secondtb1" TextWrapping="Wrap" Foreground="White" Text="{Binding Name}"/>
            </Grid>
            <Grid Column="1">
               <TextBlock x:Name="secondtb2" TextWrapping="Wrap" Foreground="White" Text="{Binding current.Extension}"/>
            </Grid>
            <Grid Column="2">
               <TextBlock x:Name="secondtb3" TextWrapping="Wrap" Foreground="White" Text="{Binding creationTime}"/>
            </Grid>
         </Grid>
      </DataTemplate>
   </ListBox.ItemTemplate>
   <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
         <EventSetter Event="MouseDoubleClick" Handler="SecondListBoxItem_MouseDoubleClick"/>
      </Style>
   </ListBox.ItemContainerStyle>
</ListBox>
dkozl
  • 32,814
  • 8
  • 87
  • 89
JaktensTid
  • 272
  • 2
  • 5
  • 20

1 Answers1

0

If you want get textbox content value:

secondtb2.Text;

You can use this method:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

and then you enumerate over the controls like so:

foreach (TextBlock textBlock in FindVisualChildren<TextBlock>(window))
{
    textBlock.Text; // your text from TextBlock
}

If you're trying to get this to work and finding that your Window (for instance) has 0 visual children, try running this method in the Loaded event handler. If you run it in the constructor (even after InitializeComponent()), the visual children aren't loaded yet, and it won't work. Link to source: Find all controls in WPF Window by type

Community
  • 1
  • 1
Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54