我正在创建一个简单的文件管理器。我尝试通过双击ListBox
打开一个新目录。如何使用secondtb1.Text
在我的代码中获取MouseDoubleClick
属性值?
我的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>
答案 0 :(得分:0)
如果您想获取文本框内容值:
secondtb2.Text;
您可以使用此方法:
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;
}
}
}
}
然后你像这样枚举控件:
foreach (TextBlock textBlock in FindVisualChildren<TextBlock>(window))
{
textBlock.Text; // your text from TextBlock
}
如果您试图让它工作并发现您的Window(例如)有0个可视子项,请尝试在Loaded事件处理程序中运行此方法。如果在构造函数中运行它(甚至在InitializeComponent()之后),则尚未加载可视子项,并且它将不起作用。 链接到来源:Find all controls in WPF Window by type