0

我有一个包含 2 个网格的 ItemsControl 的 DataTemplate。第二个网格是折叠的,当我单击 ItemsControl 中的一个项目时,我想让它可见。

这是 XAML:

<ItemsControl.Resources>
   <DataTemplate x:Key="magazineSlotTemplate">
       <Grid>
           <Grid x:Name="buttonGrid">
               <Button Margin="4" Style="{StaticResource TransparentButtonStyle}" Click="ClickEvent">
                   <Grid>
                       <Grid.ColumnDefinitions>
                           <ColumnDefinition Width="Auto"/>
                           <ColumnDefinition Width="Auto"/>
                       </Grid.ColumnDefinitions>
                       <Grid.RowDefinitions>
                           <RowDefinition Height="Auto"/>
                           <RowDefinition Height="Auto"/>
                       </Grid.RowDefinitions>
                       <Path Width="48" Height="48" Fill="{Binding Color, Mode=OneWay}" Grid.RowSpan="2" Data="M24,14z"/>
                       <Ellipse Width="20" Height="20" Fill="{Binding ColorTwo, Mode=OneWay}" Grid.RowSpan="2"/>
                       <Image Width="48" Height="48" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="2" Source="pack://application:,,,/Resources/tool.png"/>
                       <ProgressBar Orientation="Vertical" Width="15" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"/>
                       <TextBlock Grid.Column="1" Grid.Row="0" Text="24%" FontSize="11">
                           <TextBlock.LayoutTransform>
                               <RotateTransform Angle="-90"/>
                           </TextBlock.LayoutTransform>
                       </TextBlock>
                   </Grid>
               </Button>
           </Grid>

           <Grid x:Name="editGrid" Visibility="Collapsed">
               <Grid x:Name="gridMare">
                   <Grid.RowDefinitions>
                       <RowDefinition Height="Auto"/>
                       <RowDefinition Height="Auto"/>
                   </Grid.RowDefinitions>
                   <Grid Grid.Row="0">
                       <Grid.ColumnDefinitions>
                           <ColumnDefinition Width="Auto"/>
                           <ColumnDefinition Width="*"/>
                       </Grid.ColumnDefinitions>
                       <CheckBox Grid.Column="0" VerticalAlignment="Center" Margin="2,2,2,2" HorizontalAlignment="Center" IsChecked="{Binding IsPresent, Mode=OneWay}" IsEnabled="False">
                           <CheckBox.LayoutTransform>
                               <ScaleTransform ScaleX="1.6" ScaleY="1.6"/>
                           </CheckBox.LayoutTransform>
                       </CheckBox>
                       <ComboBox x:Name="cmb" Grid.Column="1" Margin="2" ItemsSource="{Binding Available, Mode=OneWay}" SelectedValue="{Binding CurrentType}"/>

                   </Grid>

这是 ceControl 是 DataTemplateSelector 和 MagazineSlotTemplate 是使用上述模板中的静态资源的 DataTemplate 背后的代码。

我可以在后面的代码中看到第二个网格,但是当我将可见性设置为可见时变得可见但属性 IsVisisble 为假,因此我看不到网格

private void ClickEvent(object sender, RoutedEventArgs e)
{
   var template = ceControl.MagazineSlotTemplate.LoadContent() as FrameworkElement;
   var myGrid = (Grid)template.FindName("editGrid");
   myGrid.Visibility = Visibility.Visible;
   var visible = myGrid.IsVisible;
}
4

1 回答 1

1

您需要获取对屏幕上可见的实际 Grid 元素的引用。您可以在可视化树中找到它。试试这个:

private void ClickEvent(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    Grid buttonGrid = button.Parent as Grid;
    if (buttonGrid != null)
    {
        Grid grid = buttonGrid.Parent as Grid;
        if (grid != null)
        {
            Grid editGrid = grid.Children.OfType<Grid>().FirstOrDefault(g => g.Name == "editGrid");
            if (editGrid != null)
            {
                editGrid.Visibility = Visibility.Visible;
            }
        }
    }
}

DataTemplate.LoadContent() 创建新元素,这些不是您在屏幕上看到的。

于 2016-12-21T11:23:32.490 回答