2012-02-07 43 views
0

我有一個列表框,我從XML文件解析信息。我爲我的ListBoxItems創建了一個自定義用戶控件,其中包含一個複選框和一個用於保存人名的文本塊。我的問題是,我無法訪問和控制代碼中的自定義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 :) 
} 

注:因爲你在這個例子中也結合成員變量的類型是ObservableCollection{T}調用Add()Remove()等,會引發事件和ListBox將進行迴應添加/刪除的UI控件這些項目是必要的。

+0

非常感謝你的回答!我會試試看看它是如何發展的。 :) – 2012-02-08 14:31:43

+0

我做了上述,但我得到一個NullReferenceException對PropertyChanged(這,新的PropertyChangedEventArgs(「OppilaanNimi」));這是我的班級現在的樣子:http://pastebin.com/T299gHuS – 2012-02-08 16:39:34

+0

哦,沒關係,我找到了解決方案:) http://pastebin.com/AC7tQDSm – 2012-02-08 16:54:26