Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

In a form, i generate several buttons, with binding.

In a XML :

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.1*" />
            <RowDefinition Height="0.85*" />
            <RowDefinition Height="0.15*" />
        </Grid.RowDefinitions>
        <ItemsControl x:Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="1" Background="{x:Null}" ItemsSource="{Binding}" Grid.ColumnSpan="4" Foreground="White">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="White" BorderThickness="1" CornerRadius="3" Margin="0,3" Grid.ColumnSpan="0" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <StackPanel Orientation="Vertical" >
                            <CheckBox x:Name="cbxLignes" Tag="{Binding strCodeLigne}" Style="{StaticResource {x:Type ToggleButton}}" Content="{Binding strCodeLigne}" FontSize="22" Margin="25,15,25,15" Width="100" Height="45" IsChecked="{Binding IsChecked}" Checked="cbxLignes_Checked" Unchecked="cbxLignes_Unchecked" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
        <Button Name="btnValider" Grid.Row="4" Grid.Column="2" Content="Valider" Background="#5491fe" Margin="95,5,5,5"  Click="btnValider_Click" />
        <Button Name="btnTous" Grid.Row="4" Grid.Column="0" Content="Tous" Background="#5491fe" Margin="95,5,5,5" Click="btnTous_Click"  />
        <Button Name="btnAucuns" Grid.Row="4" Grid.Column="1" Content="Aucun" Background="#5491fe" Margin="5,5,95,5"  />
        <Button Name="btnAnnuler" Grid.Row="4" Grid.Column="3" Content="Annuler" Background="#5491fe" Margin="5,5,95,5" Click="btnAnnuler_Click"  />
    </Grid>


My binding is generate with SQL values :

for (int i = 0; i < dvLignes.Count; i++)
            {
                clLignes = new CL_Lignes();

                //clLignes.strIDLigne = dvLignes[i]["IDLigne"].ToString().Trim();
                clLignes.strCodeLigne = dvLignes[i]["CodeLigne"].ToString().Trim();
                //clLignes.strDesLigne = dvLignes[i]["DesLigne"].ToString().Trim();

                lstLignes.Add(clLignes);

                this.DataContext = lstLignes;
            }


I need with a button, check all checkboxes generate. I don't know in advance how and which text buttons has generate

Thank you in advance.

What I have tried:

I try to find a loop, but i don't found
Posted
Updated 21-Sep-17 21:30pm

1 solution

So, you have a collection that contains, as one of its properties, a boolean called IsChecked. First of all, make sure that IsChecked is written in such a way that it raises the PropertyChanged event. Then, loop over your collection and set IsChecked = true, to check them. The binding engine will detect the PropertyChanged event being fired, and will update the value on screen as appropriate.
 
Share this answer
 
Comments
Steve15f 22-Sep-17 3:44am    
Hello

Just, how make this : Then, loop over your collection

I have add this code , when i learn for my data on my another topic

public partial class SelectionLigne : Window, INotifyPropertyChanged
{
#region Évenement qui permet de déclencher l'événement INotifyPropertyChanged et mettre à jour le binding
public void Set<tvalue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<tvalue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
{
field = newValue;

if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangedEventHandler PropertyChanged;
#endregion

private bool IsChecked;
public bool bChecked
{
get { return IsChecked; }
set { Set(ref IsChecked, value); }
}
Pete O'Hanlon 22-Sep-17 3:57am    
Where does that contain your reference to lstLignes? That's your collection as it's the one you're binding to. All you need to do is something like this:
foreach (var item in lstLignes)
{
item.IsChecked = true;
}
Steve15f 22-Sep-17 4:02am    
Great thank you

I begin to understand binding!

Thank you very much
Pete O'Hanlon 22-Sep-17 5:48am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900