【问题标题】:Frontend does not change after calling function that selects all checkboxes in a WPF调用选择 WPF 中所有复选框的函数后,前端不会更改
【发布时间】:2021-06-23 06:00:27
【问题描述】:

我正在编写的 WPF .NET Core 3.1 中存在问题。 有一个“主页”,用户可以在其中输入一些过滤器,这些过滤器将用于通过外部 webAPI 搜索某些文件;到目前为止,一切都很好。来自 webAPI 的响应是带有可用文件列表的 XML,用户必须选择下载哪些文件。为此,我有一个“弹出框”,用户可以在其中读取所有可用文件并通过复选框选择所需的文件。我必须添加一些按钮来选择/取消选择所有文件,这就是问题所在:文件被选中但前端没有注意到并继续显示为未选中。

在主页面中,我解析 XML 生成这些对象的列表:

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;
        }
    }

我用

调用弹出窗口
ListaXML l = new ListaXML(lr);
await this.ShowChildWindowAsync(l.listaXML);

其中 lr 是我找到的行列表。

弹窗后面的代码是

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;
            }
        }
    }
}

弹出窗口的 XAML 是

<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>

如果我手动选中一个复选框,一切正常,背景会发生变化,并且更改会传递回主页。如果我使用按钮,值会被更改并传递回主页,但前端没有变化,但如果我在调用页面时执行这些相同的指令,一切正常。 我错过了什么? 感谢您的帮助。

【问题讨论】:

  • 在righeNoteSpese类中实现INotifyPropertyChanged
  • 感谢您的帮助。我已经试过了,但是不仅功能不起作用,而且即使我检查一行也不起作用。

标签: c# wpf .net-core checkbox datagrid


【解决方案1】:

我没有正确实现 INotifyPropertyChanged,我将类从

public Boolean selezionato { get; set; }

到

private Boolean _selezionato;
public Boolean selezionato { 
    get { 
        return _selezionato; 
    }
    set {
        _selezionato = value;
        OnPropertyChanged("selezionato");
    }                
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-03
    • 2015-11-27
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2021-08-24
    相关资源
    最近更新 更多