【问题标题】:Windows Universal Listbox Items Acess Specific UI ElementWindows 通用列表框项目访问特定的 UI 元素
【发布时间】:2015-08-20 12:27:50
【问题描述】:

这是我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“链接”应该是Visible。我该如何做到这一点?我已经写好了循环。

for (int i = 0; i < notes.Count; i++)
{
     if (notes[i].StartsWith("http"))
     {

     }
}

【问题讨论】:

  • 创建包含可见性的数据结构是一个选项吗?另外,您的目标是哪个 Windows 版本?
  • @Jakob 看看标题:Windows 通用,所有有助于包含这个东西的东西都是一个选项;)

标签: c# xaml listbox datatemplate itemtemplate


【解决方案1】:

创建一个以文本和可见性为属性的类:

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>

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2017-03-11
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多