访问和控制自定义ListBoxItem控件
本文关键字:ListBoxItem 控件 自定义 控制 访问 | 更新日期: 2023-09-27 18:23:35
我有一个列表框,用于解析XML文件中的信息。我为我的ListBoxItems创建了一个自定义用户控件,它包括一个复选框和一个保存人名的文本块。我的问题是,我无法访问和控制代码中自定义ListBoxItem用户控件的控件。我不知道如何访问它们。我想通过按钮的点击事件来删除未检查的名称,并将已检查的名称保存到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)
{
}
}
和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自定义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>
我建议您将ListBox
绑定到一个具有布尔属性的项集合,您可以将CheckBox
的IsChecked
属性绑定到该集合;
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控件做出响应。