gpt4 book ai didi

c# - 查找放置在 ListView Wpf 中的控件

转载 作者:行者123 更新时间:2023-11-30 16:02:48 29 4
gpt4 key购买 nike

 <ListView Height="Auto"   Name="lstIndent" SelectionMode="Single" Grid.ColumnSpan="5"  HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0"   >
<ListView.View >
<GridView x:Name="dgIndentDetails" >
<GridViewColumn Width="Auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--<GroupBox Header="Department Issue Header" Grid.Row="5" Grid.ColumnSpan="5" HorizontalAlignment="Stretch" >-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" IsChecked="{Binding Path=IsIndentIssue, UpdateSourceTrigger=PropertyChanged}" x:Name="chkbxIsChecked" HorizontalAlignment="Center"/>
<Label Grid.Column="1" Grid.Row="0" Content="{Binding ItemName}" x:Name="lbllstItemName" HorizontalAlignment="Center" ></Label>
<Label Grid.Column="2" Grid.Row="0" Content="{Binding Quantity}" x:Name="lbllstQty" HorizontalAlignment="Center" ></Label>
<Label Grid.Column="3" Grid.Row="0" Content="{Binding IssueQuantity}" x:Name="lbllstIssuedQty" HorizontalAlignment="Center" ></Label>
<DataGrid Height="Auto" Padding="10,10,10,10" Width="800" Grid.Row="1" Grid.ColumnSpan="5" Name="dgIssuedItemsBatchDetails" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ItemName}">
<DataGridTextColumn.Header>
<TextBlock Text="ItemName"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding BatchNo}">
<DataGridTextColumn.Header>
<TextBlock Text="BatchNo"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Quantity}">
<DataGridTextColumn.Header>
<TextBlock Text="Quantity"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<!--</GroupBox>-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

我需要找到 DataGrid dgIssuedItemsBatchDetails 然后绑定(bind)。 DataGrid 放置在 GridView 内,GridView 放置在 ListView 内。

GridView grdvwIssueDetail = (GridView)lstIndent.FindName("dgIndentDetails");

通过使用此代码,我能够找到 GridView。我的 DataGrid 放置在 GridView DataTemplate 中。所以建议我如何从代码隐藏中找到我的 DataGrid

最佳答案

可以使用VisualTreeHelper来实现。正如 MSDN 所说:

VisualTreeHelper class provides utility methods that perform common tasks involving nodes in a visual tree.

那么让我举个例子说明它是如何实现的:

    private void SeeTheChild()
{
DataGrid myCombo = GetVisualChildInDataTemplate<DataGrid>(lstIndent);
}
private T GetVisualChildInDataTemplate<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChildInDataTemplate<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}

VisualTreeHelper.GetChildrenCount(parent) 返回 0,因为 ListView 已创建,但尚未加载。特别是 DataTemplate 还没有应用到 ListView 上,因此 Visual Tree 中什么也没有。
然后使用此方法在 WindowLoaded 事件上获取您的 DataGrid:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
SeeTheChild();
}

更新:

ListView 中查找每个项目的控件:

创建一个 SelectionChanged 事件:

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ItemContainerGenerator generator = this.listView.ItemContainerGenerator;
ListBoxItem selectedItem = (ListBoxItem)generator.ContainerFromIndex(listView.SelectedIndex);
Label aLabel = GetChildrenByType(selectedItem, typeof(Label), "label") as Label;
if (aLabel != null)
{
MessageBox.Show("We've found Label with name 'label': " + aLabel.Content);
}
else
{
MessageBox.Show("There is no such Label");
}
}

以及可以为您找到所需控件的方法:

public static Visual GetChildrenByType(Visual visualElement, Type typeElement, string nameElement)
{
if (visualElement == null) return null;
if (visualElement.GetType() == typeElement)
{
FrameworkElement fe = visualElement as FrameworkElement;
if (fe != null)
{
if (fe.Name == nameElement)
{
return fe;
}
}
}
Visual foundElement = null;
if (visualElement is FrameworkElement)
(visualElement as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualElement); i++)
{
Visual visual = VisualTreeHelper.GetChild(visualElement, i) as Visual;
foundElement = GetChildrenByType(visual, typeElement, nameElement);
if (foundElement != null)
break;
}
return foundElement;
}

关于c# - 查找放置在 ListView Wpf 中的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247724/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com