2017-10-09 12 views
0

WPFのItemsSourceプロパティ(バインディング用)に問題があります。 "ItemsSource =" {Binding} "をListBoxのプロパティとして使用しているときにプログラムを実行できません。WPFのリストボックス:ItemsSourceを使用する前にItemsコレクションを空にする必要があります

<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#の:私は私のプログラムを実行

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; } 
     } 
    } 
} 

毎回、私はこのItemsコレクションはのItemsSourceエラーを使用する前に、空である必要があり得る誰もが私の問題が何であるか

考え出しました。?
+1

「//ファイルを読み込んでいますが、これはうまく動作しますか」?そこに 'listeUtilisateurs.Items'に何かを追加していますか? –

+0

'ListeBoxUtilisateurs'に' ObservableCollection'を設定し、それ以外の方法はありません。 'ItemsSource'を' ObservableCollection'にバインドするだけです。 'ObservableCollection'にアイテムを追加し、それを削除し、それをクリアします。それに応じてListBoxも更新されます。 –

答えて

2

あなたのバインディングは、私の意見では見えません。多分あなただけのコードを落とした -

  1. あなたはDataContextColl空にしなければならないことを宣言しています。
  2. あなたは重い防ぐために呼び出しInitializeComponent()DataContextを設定する必要があります - そのDataContextに特異的に結合するよりも
  3. あなたTextBoxesを扱うDataContextChangedイベント - - 不要な操作を行うのはなぜなぜコレクションは
  4. NomTéléphoneなどのような性質を持っている必要がありますあなたはそれを使用していないときに会員SelectedItemを持っていますか?

私はあなたのウィンドウで、以下のメンバーを推薦する:たぶん

(あなたが持っているあなたのactualyタイプに変更object

public ObservableCollection<object> Coll { get; set; } 
public object SelectedItem { get; set; } 

- プロパティが内にあるかどうか、多分私が言うことができないので、リストビューのオブジェクトかどうか - すべてのテキストボックスを次のバインディングに変更します。

<TextBox x:Name="Nom" Text="{Binding SelectedItem.Nom, UpdateSourceTrigger=PropertyChanged}" /> 

ListBoxを次のように変更してください:

<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"/> 
関連する問題