[英]ItemsControl: How To Use FindName within ItemsPanelTemplate to access Panel
[英]How to access the ItemsWrapGrid panel which is placed inside the ItemsPanelTemplate?
我使用ItemsControl綁定到 ItemsSource 集合,並使用ItemsWrapGrid控制面板排列項目。 但是 ItemsWrapGrid 面板被放置在 ItemsPanelTemplate 內,所以我無法在 c# 后面的代碼中訪問該元素。
我嘗試使用VisualTreeHelper方法在可視樹中找到面板。 並且在項目面板模板中使用時不檢索元素。
<ItemsControl
x:Name="itemsControl"
ItemTemplate="{TemplateBinding ItemTemplate}"
ItemsSource="{TemplateBinding GalleryItemCollection}"
SelectedItem="{TemplateBinding SelectedItem}">
<itemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid x:Name="itemsWrapGrid"
ItemHeight="{Binding Path=ItemHeight}"
ItemWidth="{Binding Path=ItemWidth}"
MaximumRowsOrColumns="{Binding Path=MaximumColumnCount}"
Orientation="Horizontal" />
</ItemsPanelTemplate>
</itemsControl.ItemsPanel>
</itemsControl>
有人可以幫我如何訪問 c# 后面的itemswrapGrid代碼元素嗎?
您可以向ItemsWrapGrid
控件添加Loaded
事件處理程序並將發送者分配給成員變量,然后您可以通過成員變量訪問ItemsWrapGrid
控件。
例如:
//MainPage.xaml
<ItemsWrapGrid x:Name="itemsWrapGrid" Background="Red" Loaded="itemsWrapGrid_Loaded"
……> </ItemsWrapGrid>
//MainPage.xaml.cs
private ItemsWrapGrid _itemsWrapGrid;
private void itemsWrapGrid_Loaded(object sender, RoutedEventArgs e)
{
_itemsWrapGrid = sender as ItemsWrapGrid;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_itemsWrapGrid.Background = new SolidColorBrush(Colors.Blue);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.