Binding with Datagid is not working all times after adding more code lines

I can Bind an observablelist to datagrid in WPF, but I am losing Binding when adding a custom filtering functionality to Datagrid

I created a User Control which contains a DataGrid in WPF, the DataGrid is bound to variable “FilteredCollection”

        public ObservableCollection<cs_Log> FilteredCollection
        {
            get { return _filteredcollection; }
            set { _filteredcollection = value; OnPropertyChanged("FilteredCollection"); dgLogger.ItemsSource = _filteredcollection; }
        }

When I call the following function, the data is copied and datagrid is showing the new added rows correctly

        void ApplyFilter()
        {
            FilteredCollection = csGlobal.LogList;
        }

later I added a checkbox for filtering and I bound it to variable “InfoInclude”, also I modified the function

        public bool InfoInclude
        {
            get { return _infoinclude; }
            set { _infoinclude = value; OnPropertyChanged("InfoInclude"); ApplyFilter(); }
        }

        void ApplyFilter()
        {
            ObservableCollection<cs_Log> TempCollection = new ObservableCollection<cs_Log>();
            foreach (cs_Log item in csGlobal.LogList)
                if (((item.sType == "Information") && InfoInclude) || ((item.sType == "Warning") && WarnInclude) || ((item.sType == "Error") && ErrInclude))
                    TempCollection.Add(item);
            FilteredCollection = TempCollection;
            //FilteredCollection = csGlobal.LogList;
        }

the result was:

  1. When adding a new rows the “FilteredCollection” variable is already updated but the datagrid is not showing the new added rows
  2. the when checking/unchecking the checkbox, the datagrid show the filtered data on the datagrid

How can I have both adding and filtering functionality to work together as same time?

Thanks

New contributor

MKamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

From your explanations I understood: the FilteredCollection collection is loaded with a full collection of rows when the view starts; the InfoInclude, WarnInclude, ErrInclude properties are created only to manage filtering. If this is correct, then in my opinion, a more optimal implementation of filtering would be at the View level.

Look at this example (the code you didn’t show, I’m recreating it based on my guesses):

    public class LogViewModel
    {
        private GlobalModel csGlobal = new();

        public LogViewModel()
        {
            FilteredCollection = new(csGlobal.LogList);
        }

        public ObservableCollection<cs_Log> FilteredCollection { get; }

    }

Window XAML:

    <Window.DataContext>
        <local:LogViewModel/>
    </Window.DataContext>
    <UniformGrid Columns="2">
        <DataGrid x:Name="dgLog" Margin="10" ItemsSource="{Binding FilteredCollection}"/>
        <UniformGrid Columns="1">
            <CheckBox x:Name="InfoInclude"
                      Content="{Binding Name, RelativeSource={RelativeSource Self}}"
                      Click="OnRefreshFilter"
                      VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <CheckBox x:Name="WarnInclude"
                      Content="{Binding Name, RelativeSource={RelativeSource Self}}"
                      Click="OnRefreshFilter"
                      VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <CheckBox x:Name="ErrInclude"
                      Content="{Binding Name, RelativeSource={RelativeSource Self}}"
                      Click="OnRefreshFilter"
                      VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </UniformGrid>
    </UniformGrid>
</Window>

Window Code Behind:

        private void OnRefreshFilter(object sender, RoutedEventArgs e)
        {
            bool infoInclude = InfoInclude.IsChecked == true;
            bool warnInclude = WarnInclude.IsChecked == true;
            bool errInclude = ErrInclude.IsChecked == true;

            dgLog.Items.Filter = item =>
            {
                cs_Log log = (cs_Log)item;

                return log.sType switch
                {
                    "Information" => infoInclude,
                    "Warning" => warnInclude,
                    "Error" => errInclude,
                    _ => false
                };
            };
        }

P.S. And please note that binding is not just a “nice term”, but an instance of Binding set, in this case, in the value of a property in XAML.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật