如何使网格在 DataTemplateSelector 中可见
How make a grid visible in a DataTemplateSelector
我有一个包含 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 是 false 因此我看不到网格
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;
}
您需要获取对屏幕上可见的实际 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() 创建新元素,这些不是您在屏幕上看到的元素。
我有一个包含 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 是 false 因此我看不到网格
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;
}
您需要获取对屏幕上可见的实际 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() 创建新元素,这些不是您在屏幕上看到的元素。