Search code examples
wpfwpfdatagridicollectionview

Detect when DataGrid have and dont have content


I want to Bind a Button IsEnabled property to DataGrid property/Event when it has a Content. The DataGrid's Item Source is an Observable collection but Since I implement Filtering, what is displayed can be different from the ItemSource collection.

Is there an event or Property of DataGrid that gets fired when the View is empty or not empty?


Solution

  • a simple example could be made using HasItems property from DataGrid's base class ItemsControl

    HasItems counts only displayable rows after any filter

    <StackPanel>
        <DataGrid x:Name="dGrid">
            an item
        </DataGrid>
        <Button Content="a button"
                IsEnabled="{Binding HasItems,ElementName=dGrid}" />
    </StackPanel>
    

    so the HasItems from DataGrid will determine if the Button is enabled or not