我在列表视图中有以下数据模板和一些窃听事件 (下面的XAML)。为了隐藏/显示数据模板中某个项目的可见性,我使用下面的逻辑(确定我的原始源)来标识列表视图中的哪个控件调用了一个事件。我看到的这种方法的问题是,即使对数据模板进行了最小的更改,我也正在更改事件逻辑以确定合适的孩子。有没有可以使用的高效逻辑,这样,每次我更改布局时,我都不必触摸后面的代码?
XAML
<DataTemplate x:DataType="filter:FilterCategory">
<!--<RelativePanel>-->
<Grid Name="FilterHeaders">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="Header" FontSize="13" FontWeight="Bold" Margin="20,10,0,0" Text="{x:Bind NameAlias}" Grid.Column="0"/>
<RelativePanel Grid.Column="1">
<TextBlock Name="DownArrow" Text="" FontFamily="Segoe MDL2 Assets" FontSize="8"
Visibility="{x:Bind IsCollapsed}" Grid.Column="1" Tapped="DownArrow_Tapped"
RelativePanel.AlignRightWithPanel="True" HorizontalAlignment="Right" Margin="0,10,0,0"/>
<TextBlock Name="UpArrow" Text="" FontFamily="Segoe MDL2 Assets" FontSize="8"
Visibility="{x:Bind IsExpand}" RelativePanel.AlignRightWithPanel="True" Tapped="UpArrow_Tapped"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,10,0,0"/>
</RelativePanel>
<Grid Grid.Row="1" Grid.ColumnSpan="2" Visibility="{Binding ElementName=UpArrow,Path=Visibility}">
<ListView SelectionMode="None" ItemsSource="{x:Bind FilterCategoryItems}" ScrollViewer.VerticalScrollMode="Enabled">
<ListView.ItemContainerTransitions>
<TransitionCollection/>
</ListView.ItemContainerTransitions>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="FontSize" Value="13"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="Margin" Value="5,0,0,0"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="filter:FilterCategoryItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="FilterCheckBoxTask" Style="{StaticResource CheckBoxStylePlainView}" IsThreeState="False"
Opacity="0.2" MinWidth="10"/>
<TextBlock Grid.Column="1" Text="{x:Bind NameAlias}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<!--<ListView.FooterTemplate>
<DataTemplate>
<Grid>
<Button x:Name="LoadMore" Tag="{x:Bind Name}" Content="Load More" Click="LoadMore_Click" Visibility="Visible"/>
</Grid>
</DataTemplate>
</ListView.FooterTemplate>-->
</ListView>
<loading:LoadingControl Name="LoadingControl" IsShowGrid="{Binding IsShowResultGrid,Mode=TwoWay}"
IsShowProgressRing="{Binding IsShowProgressRing,Mode=TwoWay}"
ShowDisplayMessage="{Binding DisplayMessage,Mode=TwoWay}" />
</Grid>
</Grid>
</DataTemplate>
背后的代码
private void DownArrow_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
if (e.OriginalSource is TextBlock textBlock)
{
textBlock.Visibility = Visibility.Collapsed;
if (textBlock.Parent is Grid grid)
{
if (grid.Children[2] is TextBlock textBlck)
textBlck.Visibility = Visibility.Visible;
}
}
}
private void UpArrow_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
if (e.OriginalSource is TextBlock textBlock)
{
textBlock.Visibility = Visibility.Collapsed;
if (textBlock.Parent is Grid grid)
{
if (grid.Children[1] is TextBlock textBlck)
textBlck.Visibility = Visibility.Visible;
}
}
}
答案 0 :(得分:0)
您需要通过以下方式使用行为SDK:XamlBehaviors
安装nuget软件包后,可以使用以下代码:
<TextBlock x:Name="mytext" Text="1" Grid.Column="0">
<Interactivity:Interaction.Behaviors>
<Interactions:EventTriggerBehavior EventName="Tapped">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=mytext}" PropertyName="Visibility" Value="Collapsed"/>
</Interactions:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBlock>
有关更多详细信息,您可以参考此doc。
我需要承认这可能无法满足您的所有需求。实际上,您不知道以下行:grid.Children是TextBlock textBlck。我将在有时间的时候考虑使用绑定和转换器。但是在我看来,行为SDK是您的一个方向。请先看看。