dotnet / DataGridExtensions

Modular extensions for the WPF DataGrid control
MIT License
820 stars 105 forks source link

ComboBox via SelectableValues is not populated initially #15

Closed Drachenkaetzchen closed 4 years ago

Drachenkaetzchen commented 5 years ago

Hi,

I just tried your wonderful extensions. However, when trying to use a ComboBox as seen in the example, the values aren't initially populated.

Only after I type something in the ComboBox it is populated via SelectableValues. Do you have any clue on what's going on or how I can debug this?

tom-englert commented 5 years ago

Hard to tell without seeing a sample...

Brains commented 5 years ago

I've encountered the same issue. Data for DataGrid is loaded async from DB. During the loading the filtering popup obviously contains no items. But even after the data is loaded the popup contains no items. The only way to fix is to select and deselect a row (lost focus)

1

Brains commented 5 years ago

Moment when dot appeared 2

Brains commented 5 years ago

My simplified but valid sample

There are two columns with the same problem each. First uses plain ComboBoxFilter, second MultipleChoiceFilter. First uses string[] second enum[]. First binds DataGridComboBoxColumn.ItemsSource to x:Array, second to CollectionViewSource (which binds to view model with Enum.GetNames(typeof(Permission))). Initially I thought the problem was in CollectionViewSource but replacing it didn't help.

    <UserControl.Resources>
        <ResourceDictionary>
            <x:Array x:Key="Kinds" Type="system:String">
                <system:String>Admin</system:String>
                <system:String>User</system:String>
                <system:String>Undefined</system:String>
            </x:Array>
            <CollectionViewSource x:Key="Permissions" Source="{Binding Permissions}"/>
        </ResourceDictionary>
    </UserControl.Resources>

    <DataGrid 
        ItemsSource="{Binding Users}"
        AutoGenerateColumns="False"
        dgx:DataGridFilter.IsAutoFilterEnabled="True">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Type"
                                    SortMemberPath="kind"
                                    ItemsSource="{Binding Source={StaticResource Kinds}}"
                                    SelectedItemBinding="{Binding kind}">
                <dgx:DataGridFilterColumn.Template>
                    <ControlTemplate>
                        <Grid d:DataContext="{d:DesignInstance dgx:DataGridFilterColumnControl}">
                            <Grid>
                                <Control Style="{DynamicResource {x:Static dgx:DataGridFilter.IconStyleKey}}"/>
                                <ComboBox x:Name="ComboBox"
                                          Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}"
                                          ItemsSource="{Binding SourceValues}"
                                          IsEditable="True" />
                            </Grid>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="ComboBox" Property="Text" Value="">
                                <Setter TargetName="ComboBox" Property="Opacity" Value="0"/>
                            </Trigger>
                            <Trigger SourceName="ComboBox" Property="Text" Value="{x:Null}">
                                <Setter TargetName="ComboBox" Property="Opacity" Value="0"/>
                            </Trigger>
                            <Trigger SourceName="ComboBox" Property="IsMouseOver" Value="True">
                                <Setter TargetName="ComboBox" Property="Opacity" Value="1"/>
                            </Trigger>
                            <Trigger SourceName="ComboBox" Property="IsFocused" Value="True">
                                <Setter TargetName="ComboBox" Property="Opacity" Value="1"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </dgx:DataGridFilterColumn.Template>
            </DataGridComboBoxColumn>

            <DataGridComboBoxColumn Header="Permission"
                                    SortMemberPath="permission"
                                    ItemsSource="{Binding Source={StaticResource Permissions}}"
                                    SelectedItemBinding="{Binding permission}">
                <dgx:DataGridFilterColumn.Template>
                    <ControlTemplate>
                        <local:MultipleChoiceFilter Filter="{Binding Path=Filter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dgx:DataGridFilterColumnControl}}" />
                    </ControlTemplate>
                </dgx:DataGridFilterColumn.Template>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
</UserControl>
tom-englert commented 5 years ago

@Brains can you provide a PR that adds the working sample to the sample project as a new tab? Probably that's the important part that missing if you provide only some detached xaml: "... Data for DataGrid is loaded async from DB."

Brains commented 5 years ago

Sure, will try. My model is written in F# but I will do something similar in C#

Brains commented 5 years ago

However, I tried to make PR yesterday for my other ticket #19, but had not enough rights. Can you add me in repo settings somehow?

tom-englert commented 5 years ago

@Brains you have to work in your own fork, then you can create a PR from there

Brains commented 5 years ago

Finally, done #20

tom-englert commented 5 years ago

Maybe you should change your code to not replace the complete datasource but bind it to an observable collection that is synchronized with the items from the db - this can solve such problems.

Brains commented 5 years ago

Sorry, forgot to answer. Ok will try. I am working with F#, it produces a new collection each time, so I just used functional style. Will try observable collection instead

Brains commented 4 years ago

Ok will try.

Sorry for so long delay. Just tried. Surprisingly, but ObservableCollection didn't help, see my commit 8a65051848bc06b0966e4c6d4ada2a135f15c20b.