I'd like to return only the Content of checked boxes in a checkbox list that is defined this way in XAML :
<Grid>
<StackPanel Width="250" Height="80"></StackPanel>
<TextBox Name="ZoneText" Width="160" Height="20" Margin="12,215,330,76" Background="Bisque" />
<TextBox Name="ZoneValue" Width="160" Height="20" Margin="12,239,330,52" Background="Bisque" />
<ListBox Name="listBoxZone" ItemsSource="{Binding TheList}" Background="Azure" Margin="12,12,12,115">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone" Content="{Binding TheText}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Validate" Height="44" HorizontalAlignment="Left" Margin="362,215,0,0" Name="button1" VerticalAlignment="Top" Width="123" Click="button1_Click" />
</Grid>
I'd like to be able to store all the content of the checked checkboxes in a list (when I press a button), but I can't seem to do it, here is what I've tried in C# :
public MainWindow()
{
InitializeComponent();
CreateCheckBoxList();
}
public class BoolStringClass
{
public string TheText { get; set; }
public int TheValue { get; set; }
}
public void CreateCheckBoxList()
{
//some code that creates a list
}
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox item in listBoxZone.Items)
{
List<string> selectedFOP = new List<String>();
if (item.IsChecked == true)
{
selectedFOP.Add(item.Content.ToString());
}
}
}
But I get an error that it is impossible to do a cast of an object whose type is 'BoolStringClass' into a 'System.Windows.Controls.CheckBox'. I guess I have to use listBoxZone.ItemTemplate instead, but how can I access the checkboxes in it ? Thank you
Aucun commentaire:
Enregistrer un commentaire