Windows通用列表框项目访问特定的UI元素 [英] Windows Universal Listbox Items Acess Specific UI Element
本文介绍了Windows通用列表框项目访问特定的UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 ListBox
的xaml代码:
Here is the xaml code of my ListBox
:
<ListBox x:Name="BoardList" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBox IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding}" TextWrapping="Wrap" Foreground="DarkBlue"></TextBox>
<AppBarButton Visibility="Collapsed" Icon="Globe" Click="OpenInBrowser" x:Name="Link"></AppBarButton>
<AppBarButton Icon="Copy" Click="Copy"></AppBarButton>
<AppBarButton Icon="Delete" Click="Delete"></AppBarButton>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ItemSource
绑定到一个简单列表称为 notes
的字符串。
现在,我检查笔记
是否以 http
开头,是否包含该特定项目的AppBarButton
链接应为可见
。我该如何实现?我已经写了循环。
The ItemSource
gets binded to a simple list of strings which is called notes
.
Now I check if the note
begins with http
and if it does the AppBarButton
"link" for this specific item should be Visible
. How do I achieve that? I already wrote the loop.
for (int i = 0; i < notes.Count; i++)
{
if (notes[i].StartsWith("http"))
{
}
}
推荐答案
创建一个具有文本和可见性作为属性的类:
Create a class with text and visibility as properties:
public class CustomObject
{
public CustomObject(string text)
{
this.text = text;
}
public string text { get; set; }
public Visibility visibility
{
get
{
if (text.StartsWith("http"))
return Visibility.Visible;
else
return Visibility.Collapsed;
}
}
}
设置您的 ItemsSource
到CustomObjects列表。
Set your ItemsSource
to a list of CustomObjects.
分别在xaml文件中将绑定设置为文本和可见性:
Set your Bindings to text and visibility respectively in the xaml file:
<ListBox x:Name="BoardList" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBox IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding text}" TextWrapping="Wrap" Foreground="DarkBlue"></TextBox>
<AppBarButton Visibility="{Binding visibility}" Icon="Globe" Click="OpenInBrowser"></AppBarButton>
<AppBarButton Icon="Copy" Click="Copy"></AppBarButton>
<AppBarButton Icon="Delete" Click="Delete"></AppBarButton>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这篇关于Windows通用列表框项目访问特定的UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文