vendredi 26 mars 2021

Frontend does not change after calling function that selects all checkboxes in a WPF

I have a problem in a WPF .NET Core 3.1 that I am writing. There is a 'Main Page', where the user can input some filters that will be used to search for some files by an external webAPI; so far so good. The response from the webAPI is an XML with the list of the files available and the user must choose which of these files download. To do so, I have a 'popup box' where the user can read all the available files and selected the desired ones by checkboxes. I have to add some buttons to select / deselect all the files and here lies the problem: the files are selected but the fronted does not notice and keep showing them as unchecked.

In the main page, parsing the XML I generate a List of these objects:

public class righeNoteSpese {
        public Boolean selezionato { get; set; }
        public Boolean isOK { get; set; }
        public String errore { get; set; }
        // Other String fields...

        public righeNoteSpese() {
           selezionato = false;
           isOK = true;
           errore = String.Empty;
        }
    }

and I call the popup with

ListaXML l = new ListaXML(lr);
await this.ShowChildWindowAsync(l.listaXML);

where lr is the list of rows I found.

The code behind of the popup is

public partial class ListaXML : ChildWindow
{
    public List<righeNoteSpese> Elenco = new List<righeNoteSpese>();

    public ListaXML()
    {
        InitializeComponent();
    }

    public ListaXML(List<righeNoteSpese> listF) {
        InitializeComponent();

        this.DataContext = this;
        
        Elenco = listF;
        
        selFiles.ItemsSource = listF;

        /* If not commented the foreach works and all the rows are checked!
        foreach (righeNoteSpese r in Elenco)
        {
            if (r.isOK)
            {
                r.selezionato = true;
            }
        }*/
    }

    private void All_Click(object sender, RoutedEventArgs e)
    {
        foreach (righeNoteSpese r in Elenco) {
            if (r.isOK)
            {
                r.selezionato = true;
            }
        }
    }
}

The XAML of the popup is

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Name="Btn1" Width="100" Content="Select All"  Grid.Row="0" Grid.Column="0" Margin="10 15 10 15" Click="All_Click" />
    <DataGrid Name="selFiles" AutoGenerateColumns="False" CanUserAddRows="false" HorizontalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" AlternatingRowBackground="LightGray" Grid.Row="1" Grid.ColumnSpan="4">
        <DataGrid.Columns><DataGridTextColumn Header="Errore" Width="200" Binding="{Binding errore, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding isOK}" Value="False">
                                <Setter Property="Background" Value="Red"/>
                                <Setter Property="FontStyle" Value="Italic" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding selezionato}" Value="True">
                                <Setter Property="Background" Value="SpringGreen"/>
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

            <DataGridTemplateColumn Width="SizeToHeader" IsReadOnly="True" Header="Select">
                <DataGridTemplateColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding isOK}" Value="False">
                                <Setter Property="Background" Value="Red"/>
                                <Setter Property="FontStyle" Value="Italic" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding selezionato}" Value="True">
                                <Setter Property="Background" Value="SpringGreen"/>
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTemplateColumn.CellStyle>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding selezionato, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2,0,2,0" IsEnabled="{Binding isOK, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <!-- Other columns... -->

        </DataGrid.Columns>
    </DataGrid>


</Grid>

If I check manually a checkbox everything works, the background changes and the change is passed back to the main page. If I use the button the values are changed and passed back to the main page but there is no change in the frontend but if I execute these same instructions when I call the page everything is OK. What am I missing? Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire