2012-02-07 28 views
0

XMLファイルから情報を解析するリストボックスがあります。私はチェックボックスと人々の名前を保持するためのテキストブロックで構成されるListBoxItemのカスタムコントロールを作成しました。私の問題は、私はアクセスできず、コード内のカスタムListBoxItemユーザーコントロールのコントロールを制御できないということです。私はそれらにアクセスする方法を知らない。チェックされていない名前を削除し、チェックされた名前をIsolatedStorageのファイルに保存するボタンのクリックイベントが必要です。私はまだ基​​本的な "選択されたチェックボックスの識別"を取得したいので、データの保存には取り組んでいません。私を助けてくれますか?カスタムListBoxItemコントロールへのアクセスとコントロール

ここでは、コードです:

public partial class OppilasLista : PhoneApplicationPage 

    { 
     XDocument lista = XDocument.Load("NykyisetKurssit.xml"); 
     XDocument oppilasInfo = XDocument.Load("Oppilaat.xml"); 
     string id = string.Empty; 

     public OppilasLista() 
     { 
      InitializeComponent(); 

     } 

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
     { 
      if (NavigationContext.QueryString.TryGetValue("id", out id)) 
      { 

       var ryhma = (from ryhmaInfo in lista.Descendants("Kurssi") 
          where ryhmaInfo.Attribute("id").Value == id 
          select new Kurssit 
          { 
           RyhmanNimi = (string)ryhmaInfo.Element("tunnus").Value 

          }).FirstOrDefault(); 

       PageTitle.Text = ryhma.RyhmanNimi; 

       var oppilas = (from oppilaat in oppilasInfo.Descendants("Oppilas") 
           where oppilaat.Attribute("ryhma").Value == id 
           select new Kurssit 
           { 
            OppilaanNimi = (string)oppilaat.Element("nimi").Value 
           }); 

       oppilaidenLista.ItemsSource = oppilas; 

      } 
      base.OnNavigatedTo(e); 
     } 

     private void Tallenna_Button_Click(object sender, RoutedEventArgs e) 
     { 

     } 

    } 

とXAML

<!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock x:Name="ApplicationTitle" Text="LÄSNÄOLOT" Style="{StaticResource PhoneTextNormalStyle}"/> 
      <TextBlock x:Name="PageTitle" Text="{Binding RyhmanNimi}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
      <ListBox ItemsSource="{Binding}" x:Name="oppilaidenLista" Margin="0,0" Height="500" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <local:ListboxItem /> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
      <Button Content="Tallenna" Height="72" HorizontalAlignment="Left" Margin="12,506,0,0" Name="tallennaButton" VerticalAlignment="Top" Width="438" Click="Tallenna_Button_Click" /> 
     </Grid> 
    </Grid> 

とカスタムListBoxItemコントロール

<Grid x:Name="LayoutRoot"> 
     <CheckBox Height="72" HorizontalAlignment="Left" Margin="0,7,0,0" Name="checkBox" VerticalAlignment="Top" /> 
     <TextBlock Height="55" HorizontalAlignment="Left" Margin="74,12,0,0" Name="studentName" Text= "{Binding OppilaanNimi}" VerticalAlignment="Top" Width="394" FontSize="40" /> 
    </Grid> 

答えて

1

私はあなたがどのアイテムのコレクションにあなたのListBoxに結合することをお勧めしたいが、バインドできるブール値のプロパティを持っているCheckBoxIsCheckedにプロパティ。 〜の行に沿って何か。

public class Kurssit : INotifyPropertyChanged { 
    private string _Oppilaanimi; 
    private bool _IsChecked; 

    public string OppilaanNimi { 
    get { return _Oppilaanimi; } 
    set { 
     if (_Oppilaanimi != value) { 
     _Oppilaanimi = value; 
     PropertyChanged(this, new PropertyChangedEventArgs("OppilaanNimi")); 
     } 
    } 

    public bool IsChecked { 
    get { return _IsChecked ; } 
    set { 
     if (_IsChecked != value) { 
     _IsChecked = value; 
     PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

あなたが言う、メンバ変数に保存し、あなたのLINQ文でこれらの項目を作成

private ObservableCollection<Kurssit> _Kurssit

そして、このメンバー変数にListBoxをバインドします。

oppilaidenLista.ItemsSource = _Kurssit; 

さらに、カスタムListBoxItemコントロールを次のように変更します。

<Grid x:Name="LayoutRoot"> 
    <CheckBox IsChecked="{Binding IsChecked}" Height="72" HorizontalAlignment="Left" Margin="0,7,0,0" Name="checkBox" VerticalAlignment="Top" /> 
    <TextBlock Height="55" HorizontalAlignment="Left" Margin="74,12,0,0" Name="studentName" Text= "{Binding OppilaanNimi}" VerticalAlignment="Top" Width="394" FontSize="40" /> 
</Grid> 

ボタンのクリックハンドラーで、

private void Tallenna_Button_Click(object sender, RoutedEventArgs e) 
{ 
    List<Kurssit> selected = _Kurssit.Where(x => x.IsSelected == true).ToList() 
    // You can now operate manipulate this sublist as needed 
    // ie. Save this subset of items to IsolatedStorage 
    //  Or remove the items from the original _Kurssit collection... whatever :) 
} 

注:イベントを発生させますとListBoxはのためのUIコントロールを追加/削除、これらに対応させていただきます、ら、あなたはこの例では、あまりにも結合しているメンバ変数がAdd()Remove()を呼び出すタイプObservableCollection{T}であるので必要に応じてそれらの項目。

+0

ありがとうございます。私はそれを試して、それがどのように行くか見る。 :) –

+0

私は上記を行いましたが、PropertyChanged(これは新しいPropertyChangedEventArgs( "OppilaanNimi"))でNullReferenceExceptionが発生しました。これは私のクラスが今見える方法です:http://pastebin.com/T299gHuS –

+0

ああ、私は解決策を見つけました:) http://pastebin.com/AC7tQDSm –

関連する問題