首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF中的Listbox :在使用ItemsSource之前项集合必须为空

WPF中的Listbox :在使用ItemsSource之前项集合必须为空
EN

Stack Overflow用户
提问于 2017-10-09 18:18:46
回答 1查看 2.1K关注 0票数 0

我对WPF中的ItemsSource属性有一些问题(用于绑定)。当使用“ItemsSource=”{ fine }作为ListBox的属性时,我无法运行程序。虽然,如果删除它,程序运行良好,但是绑定不能工作。

XAML:

代码语言:javascript
运行
复制
<Window x:Class="TP1.EcranPrincipal"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TP1"
        mc:Ignorable="d"
        Background="CornflowerBlue"
        Title="" Height="400" Width="700">

    <Window.Resources>
        <DataTemplate x:Key="masterTemplate">
            <TextBlock
          Margin="4"
          Text="{Binding Description,UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="1" Grid.Row="0" Padding="5" VerticalContentAlignment="Center" Margin="10,10,90,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Grid.ColumnSpan="4"/>
        <Button x:Name="BoutonRechercher" Grid.Column="4" Grid.Row="0" Padding="5" Content="Rechercher" HorizontalAlignment="Left" Margin="88,9,0,0" VerticalAlignment="Top" Click="BoutonRechercher_Click" Width="75"/>

        <!-- 1st TextBoxes' column -->
        <TextBox x:Name="Nom" Text="{Binding Nom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="1" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Telephone" Text="{Binding Téléphone,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="2" Margin="0, 0, 10, 10"  VerticalContentAlignment="Center" Padding="5,0" />
        <TextBox x:Name="Adresse1" Text="{Binding Adresse1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="3" Margin="0, 0, 10, 10" Grid.ColumnSpan="3" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Adresse2" Text="{Binding Adresse2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="4" Margin="0, 0, 10, 10" Grid.ColumnSpan="3" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Ville" Text="{Binding Ville,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="5" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Pays" Text="{Binding Pays,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="6" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>

        <!-- 2nd TextBoxes' column -->
        <TextBox x:Name="Prenom" Text="{Binding Prénom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="1" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Courriel" Text="{Binding Courriel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="2" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>
        <TextBox x:Name="Province" Text="{Binding Province,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="5" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>
        <TextBox x:Name="CodePostal" Text="{Binding CodePostal,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="6" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/>

        <ListBox x:Name="ListeBoxUtilisateurs" ItemsSource="{Binding}" ItemTemplate="{StaticResource masterTemplate}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Height="349" Margin="10,10,0,0" VerticalAlignment="Top" Width="153" Grid.RowSpan="7"/>


    </Grid>
</Window>

C#:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

// StreamReader/Writer:
using System.IO;
using System.ComponentModel;

namespace TP1
{
    /// <summary>
    /// Logique d'interaction pour EcranPrincipal.xaml
    /// </summary>
    public partial class EcranPrincipal : Window
    {

        // Required variables to read the file
        List<String> elementsLignes = new List<String>();
        List<Utilisateur> utilisateurs = new List<Utilisateur>();
        Collection Coll = new Collection();



        public EcranPrincipal()
        {
            InitializeComponent();
            this.DataContext = Coll;
            ListBox listeUtilisateurs = ListeBoxUtilisateurs;

            var reader = new StreamReader(File.OpenRead("TextFile.txt"));
            int nbUtilisateurs = 0;

            Console.WriteLine(reader.ReadLine());

            while (reader.ReadLine() != null)
            {
               // Reading the file, this works fine
            }
        }


        private ListBoxItem selectedItem;
        public ListBoxItem SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; }
        }
    }
}

每次运行我的程序时,在使用ItemsSource错误之前,我都会得到这个项集合必须为空。有人知道我的问题是什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 18:35:37

在我看来,你的装订似乎是脱节的。

  1. 您正在声明DataContext将是一个空的Coll --也许您只是删除了代码。
  2. 应该在调用DataContext之前设置InitializeComponent(),以防止不必要的- DataContextChanged事件处理。
  3. 您的TextBoxes而不是绑定到该DataContext --为什么集合应该具有NomTéléphone等属性
  4. 为什么您有成员SelectedItem,而您从未使用它?

我将在您的窗口推荐下列成员:

代码语言:javascript
运行
复制
public ObservableCollection<object> Coll { get; set; }
public object SelectedItem { get; set; }

(将object更改为您的实际类型)

也许--也许是因为我不知道这些属性是否在listview的对象中--更改了所有的TextBoxes,使其具有以下绑定:

代码语言:javascript
运行
复制
<TextBox x:Name="Nom" Text="{Binding SelectedItem.Nom, UpdateSourceTrigger=PropertyChanged}" />

将您的ListBox更改为:

代码语言:javascript
运行
复制
<ListBox x:Name="ListeBoxUtilisateurs" ItemsSource="{Binding Coll}" SelectedItem="{Binding SelectedItem}" ItemTemplate="{StaticResource masterTemplate}" HorizontalAlignment="Left" Height="349" Margin="10,10,0,0" VerticalAlignment="Top" Width="153" Grid.RowSpan="7"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46652668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档