如何从WPF中的CS文件中调用网格

时间:2019-08-01 09:00:51

标签: wpf xaml grid

再也无法在列表框内调用网格了……我的xaml如下。

<UserControl x:Class="WPFPurpleButtonTest.InstrumentUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFPurpleButtonTest"
             mc:Ignorable="d" 
             d:DesignHeight="750" d:DesignWidth="900">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="706" Margin="24,34,0,-212" VerticalAlignment="Top" Width="850" Grid.RowSpan="2">
            <TabItem Header="TabItem" Name="mainTab">
                <Grid Background="#FFE5E5E5" Margin="0,0,-396,-255">
                    <Label x:Name="colourName" Content="PURPLE" HorizontalAlignment="Left" Height="93" Margin="284,88,0,0" VerticalAlignment="Top" Width="243" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="50" Foreground="#FFDC00FF"/>
                    <Button x:Name="testButton" Content="Button" HorizontalAlignment="Left" Margin="365,181,0,0" VerticalAlignment="Top" Width="75" Click="TestButton_Click"/>
                    <Label x:Name="label" Content="Row Size" HorizontalAlignment="Left" Margin="198,211,0,0" VerticalAlignment="Top" Foreground="#FFDC00FF"/>
                    <Label x:Name="label_Copy" Content="Column Size" HorizontalAlignment="Left" Margin="432,211,0,0" VerticalAlignment="Top" Foreground="#FFDC00FF"/>
                    <Button x:Name="createGrid" Content="Create Grid" HorizontalAlignment="Left" Margin="365,273,0,0" VerticalAlignment="Top" Width="75" Click="CreateGrid_Click"/>
                    <TextBox x:Name="rowSizeText" HorizontalAlignment="Left" Height="23" Margin="278,214,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="62"/>
                    <TextBox x:Name="columnSizeText" HorizontalAlignment="Left" Height="23" Margin="522,215,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="62"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem" Name="gridTab">
                <ListBox x:Name="listbox1" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel IsItemsHost="True" Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="20" HorizontalAlignment="Center">
                                <Viewbox>
                                    <Grid x:Name="wellGrid" Grid.Row="1" ShowGridLines="True"
                      local:GridHelpers.RowCount="{Binding RowCount}"
                      local:GridHelpers.ColumnCount="{Binding ColumnCount}" Margin="15,15,15,15" />
                                </Viewbox>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </TabItem>
        </TabControl>
    </Grid>
</UserControl>

我希望能够调用wellGrid。像以前那样的孩子,当网格不在列表框中时,但现在还不完全确定如何将其放在列表框中。

1 个答案:

答案 0 :(得分:0)

应用了Grid is part of the DataTemplate that is assigned to the ListBoxItem . You need to get the ContentPresenter of the item. This is where the DataTemplate`。

private void Button_Click(object sender, RoutedEventArgs e)
{
  int selectedItemIndex = this.listbox1.SelectedIndex;
  if (selectedItemIndex == -1)
  {
    return;
  }

  var itemContainer = this.listbox1.ItemContainerGenerator.ContainerFromIndex(selectedItemIndex) as ListBoxItem;

  if (TryFindChildElement(itemContainer, out ContentPresenter contentPresenter))
  {
    // Get the Grid
    var grid = contentPresenter.ContentTemplate.FindName("wellGrid", myContentPresenter) as Grid;    
  }
}

// Helper to traverse the visual tree
private bool TryFindChildElement<TElement>(DependencyObject parent, out TElement resultElement) where TElement : DependencyObject
{
  resultElement = null;
  for (var childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); childIndex++)
  {
    DependencyObject childElement = VisualTreeHelper.GetChild(parent, childIndex);

    if (childElement is Popup popup)
    {
      childElement = popup.Child;
    }

    if (childElement is TElement)
    {
      resultElement = childElement as TElement;
      return true;
    }

    if (TryFindChildElement(childElement, out resultElement))
    {
      return true;
    }
  }

  return false;
}