gpt4 book ai didi

c# - 列表框内元素的唯一名称

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:49 24 4
gpt4 key购买 nike

我遇到了一个问题,我需要在 ListBox 中命名一个 UIElement,但我不能这样做,因为它会循环并且显然不能让一百万个小 TextBlock 以相同的名称运行.

这是我的 ListBox 的简化版本:

<ListBox Background="Transparent">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<RelativePanel">
<TextBlock FontSize="12" Text="{Binding Path=Hub}" FontWeight="Bold" Name="firstText" />
<TextBlock FontSize="12" Text="{Binding Path=Stats.Pages}" Name="secondText" RelativePanel.RightOf="firstText" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="1100" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="secondText.(RelativePanel.RightOf)" Value="hubText" />
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="secondText.(RelativePanel.Below)" Value="firstText" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</RelativePanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

对于我的情况,我尝试在 RelativePanel 上使用 AdaptiveTriggers 以在屏幕较宽时将 secondText TextBlock 的位置设置到 firstText 的右侧,当屏幕较小时在下方。

据我所知,我可以更改它的唯一方法是在名称上使用 Setter,所以我需要一个独特的方法吗?有更好的方法吗?

编辑: 好的,所以它编译并运行良好(RelativePanel.RightOf 最初工作),但 AdaptiveTriggers 不起作用,VS 对标记提出了质疑。我能做什么?

最佳答案

诀窍是用 UserControl 将元素包装在 DataTemplate 中。

<ListBox.ItemTemplate>
<DataTemplate>
<UserControl>
<RelativePanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Wide">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="1100" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="secondText.(RelativePanel.RightOf)" Value="hubText" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Normal">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="secondText.(RelativePanel.Below)" Value="firstText" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBlock FontSize=" 12" Text="hub" FontWeight="Bold" x:Name="hubText" />
<TextBlock FontSize="12" Text="first" x:Name="firstText" RelativePanel.RightOf="hubText" />
<TextBlock FontSize="12" Text="second" x:Name="secondText" RelativePanel.RightOf="firstText" />
</RelativePanel>
</UserControl>
</DataTemplate>
</ListBox.ItemTemplate>

请注意,我还将 TextBlockName 更改为 x:Name

关于c# - 列表框内元素的唯一名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com