如何将文本框的Text属性与UWP中的ListView项进行比较

时间:2016-08-24 08:29:13

标签: uwp uwp-xaml

我正在撰写 UWP 应用。在其中我有一个 MainPage 。在 MainPage 内,splitview内有splitview.content框架。我将框架导航到各个页面。在一个页面中,我有一个网格。在网格中,我有三个rows。中间一行有ListView。这里出现了问题。 ListView工作正常。但突然由于未知原因,物品之间的差距变得很长。前两个items都可以。它们在屏幕上可见。但是第三个及以后的所有items都在下面,且幅度非常大。即使更改ListViewListViewItems的边距也不会改变任何内容。 ListViewObservable collection绑定。 XAML代码是:

<ListView Grid.Row="1" x:Name="AccountsList"   ItemsSource="{x:Bind accounts}" >
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="data:Account">
                        <TextBlock Text="{x:Bind Name}" Margin="0,0,0,0" >
                        </TextBlock>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

问题的原因在code behind。用户在textbox中撰写文字,然后点击Add Button。我尝试从text的每个ListViewitem或数据绑定属性访问ObservableCollection,并将其与用户提供的Text进行比较,以便添加到ObservableCollection。如果textsame,则流程将中止,否则该项目会添加到ObservableCollection。 c#代码是

if (AccountsList.Items.Any())
        {
            string[] newName =AccountsList.Items.Select(x=>x.ToString()).ToArray();
            int itemCount = newName.Count();
            CountTextBlock.Text = itemCount.ToString();
            for (int j = 0; j <= itemCount; j++)
                foreach(var item in newName )
                if (NameTextBox.Text.ToString() == item)
                {
                    await new Windows.UI.Popups.MessageDialog("Name already added- Write a new name in the Text box to Add").ShowAsync();
                }
                else
                {

                    accounts.Add(new Account { Name = NameTextBox.Text });
                    NameTextBox.Text = "";
                }
        }
        else
        {

            accounts.Add(new Account { Name = NameTextBox.Text });
            NameTextBox.Text = "";
        }

使用CountTextBlock进行测试。CountTextBlock.Text在添加second item时显示 1 **。添加third item时** ** ** fourth item添加时** ** **。{255} 添加fifth item时添加addition 底边距会随着每个新sixth item而增加。当我尝试添加List<String> someStrings = ... assertThat(someStrings).isSorted(); 时,&#34; OutOfMemoryException&#34; 被抛出。我只想比较列表视图收藏 ListView DataBounded 文本框以避免重复

2 个答案:

答案 0 :(得分:0)

哟可以试试这种组合。

ListViewItemPresenter

<Style x:Key="ListViewItemPresenterStyle" TargetType="ListViewItemPresenter">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="ContentMargin" Value="0"/>
        <Setter Property="Margin" Value="0"/>
    </Style>

ListViewItem的

<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="IsHoldingEnabled" Value="True"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <ListViewItemPresenter CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}"
                                           CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}"
                                           ContentTransitions="{TemplateBinding ContentTransitions}"
                                           CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"
                                           DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
                                           DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                                           DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
                                           DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                                           FocusBorderBrush="{ThemeResource ListViewItemFocusBorderThemeBrush}"
                                           Padding="{TemplateBinding Padding}" PointerOverBackgroundMargin="1"
                                           PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                                           PointerOverBackground="Transparent"
                                           ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                                           SelectedPointerOverBorderBrush="Transparent"
                                           SelectionCheckMarkVisualEnabled="True"
                                           SelectedForeground="Transparent"
                                           SelectedPointerOverBackground="Transparent"
                                           SelectedBorderThickness="0"
                                           SelectedBackground="Transparent"
                                           Style="{StaticResource ListViewItemPresenterStyle}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ListView控件

<ListView ItemContainerStyle="{StaticResource ListViewItemStyle}"></ListView>

如您所见,您可以在两种样式,Presenter和item中设置边距。试着告诉我这是否能解决问题。

答案 1 :(得分:0)

我能够从其他资源中找到答案。正确的代码块是:

namespace ListStorageAccntFiles
{
   class Program
   {
      static void Main(string[] args)
      {
        Console.Clear();

        CloudStorageAccount StorageAccount =
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        var BlobClient = StorageAccount.CreateCloudBlobClient();
        var Container = BlobClient.GetContainerReference("samples‐workitems");

       //Code to list the blobnames in Console
       var list = Container.ListBlobs();
       List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b =>b.Name).ToList();

       blobNames.ForEach(Console.WriteLine);
       //Code for the remaining

    }
  }
}

我在将 if (AccountsList.Items.Any()) { int count = AccountsList.Items.Count(); int check = 0; for (int i = 0; i <= count - 1; i++) { if (NameTextBox.Text == (AccountsList.Items[i] as Account).Name) { await new Windows.UI.Popups.MessageDialog("Name already added- Write a new name in the Text box to Add").ShowAsync(); break; } check += 1; if (check == count) accounts.Add(new Account { Name = NameTextBox.Text }); } } else { accounts.Add(new Account { Name = NameTextBox.Text }); } NameTextBox.Text = string.Empty; TextBox.Text进行比较时出现问题。以下代码解决了这个问题

ListView.Items

现在我可以比较两个值并仅在它是唯一的时候输入新值以避免重复。