Windows 通用列表框项目访问特定 UI 元素
Windows Universal Listbox Items Acess Specific UI Element
这是我的 ListBox
的 xaml 代码:
<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
。
现在我检查 note
是否以 http
开头,如果是 AppBarButton
"link" 则此特定项目应该是 Visible
。我该如何实现?我已经写好了循环。
for (int i = 0; i < notes.Count; i++)
{
if (notes[i].StartsWith("http"))
{
}
}
创建一个 class 将文本和可见性作为属性:
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
设置为自定义对象列表。
在 xaml 文件中分别将绑定设置为文本和可见性:
<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>
这是我的 ListBox
的 xaml 代码:
<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
。
现在我检查 note
是否以 http
开头,如果是 AppBarButton
"link" 则此特定项目应该是 Visible
。我该如何实现?我已经写好了循环。
for (int i = 0; i < notes.Count; i++)
{
if (notes[i].StartsWith("http"))
{
}
}
创建一个 class 将文本和可见性作为属性:
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
设置为自定义对象列表。
在 xaml 文件中分别将绑定设置为文本和可见性:
<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>