我有一个列表框,我从XML文件中解析信息。我为ListBoxItems创建了一个自定义用户控件,它包含一个复选框和一个用于保存人名的文本块。我的问题是我无法访问和控制代码中自定义ListBoxItem用户控件的控件。我不知道如何访问它们。我想按一下按钮的click事件来删除未检查的名称,并将已检查的名称保存到IsolatedStorage中的文件中。我还没有保存数据,因为我想先得到基本的“选中的复选框标识”。你能救我一下吗?
以下是代码:
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)
{
}
}
AND 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>
AND Custom 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>
答案 0 :(得分:1)
我建议您将ListBox
绑定到具有布尔属性的项集合,您可以绑定CheckBox
的{{1}}属性。有些东西;
IsChecked
在LINQ语句中创建这些项目时,将它们存储在成员变量中,例如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;
}
。
然后将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>
注意:因为在此示例中您绑定的成员变量类型为ObservableCollection{T},调用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 :)
}
,Add()
等等将引发事件和{{1}将根据需要响应这些添加/删除这些项的UI控件。