WPF ListViewItem DataTemplate child visibility only updates on click

22 views Asked by At

I have a ListView that uses a DataTemplate with itens that have visibility property bound to a source item property. The binding is working but only updates when I click the listitem in runtime. I reduced the datatemplate to a simple test:

        <DataTemplate x:Key="ClienteListItemTemplate" d:DataType="{x:Type viewModels:ClienteViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock
                    VerticalAlignment="Center"
                    Text="{Binding OrcamentosPendentes}"
                    Visibility="{Binding OrcamentosPendentes, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NotZeroToVisibilityConverter}}" />
                <iconPacks:PackIconMaterial Kind="HeadQuestion">
                    <iconPacks:PackIconMaterial.Style>
                        <Style TargetType="{x:Type iconPacks:PackIconMaterial}">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding OrcamentosPendentes, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NotZeroConverter}}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </iconPacks:PackIconMaterial.Style>
                </iconPacks:PackIconMaterial>
            </StackPanel>
        </DataTemplate>

I tried using direct binding in the Visibility property and also using a DataTrigger in the Style of the object and they have the same Effect.

The ListView object is very simple also:

                <ListView
                    Grid.Row="1"
                    d:ItemsSource="{d:SampleData ItemCount=5}"
                    ItemTemplate="{StaticResource ClienteListItemTemplate}"
                    ItemsSource="{Binding Clientes, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=False}"
                    SelectedItem="{Binding ClienteSelecionado, Mode=TwoWay}">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>

The C# code:

ItemsSource:

public ObservableCollection<ClienteViewModel> Clientes { get; } = new ObservableCollection<ClienteViewModel>();
        

ClienteViewModel:

    public class ClienteViewModel : ObservableValidator
    {
        ...
        private int _orcamentosPendentes;
        public int OrcamentosPendentes
        {
            get { return _orcamentosPendentes; }
            set { SetProperty(ref _orcamentosPendentes, value); }
        }
    }

I tried changing the Bindings with and without the UpdateSourceTrigger=PropertyChanged inside the DataTemplate and also in the ItemsSource of the ListView and no changes either.

Is there something I'm forgetting? Something wrong in the code? Should I use any other option than ListView?

Any help is appreciated.

I have a ListView that uses a DataTemplate with itens that have visibility property bound to a source item property. The binding is working but only updates when I click the listitem in runtime. I reduced the datatemplate to a simple test:

        <DataTemplate x:Key="ClienteListItemTemplate" d:DataType="{x:Type viewModels:ClienteViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock
                    VerticalAlignment="Center"
                    Text="{Binding OrcamentosPendentes}"
                    Visibility="{Binding OrcamentosPendentes, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NotZeroToVisibilityConverter}}" />
                <iconPacks:PackIconMaterial Kind="HeadQuestion">
                    <iconPacks:PackIconMaterial.Style>
                        <Style TargetType="{x:Type iconPacks:PackIconMaterial}">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding OrcamentosPendentes, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NotZeroConverter}}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </iconPacks:PackIconMaterial.Style>
                </iconPacks:PackIconMaterial>
            </StackPanel>
        </DataTemplate>

I tried using direct binding in the Visibility property and also using a DataTrigger in the Style of the object and they have the same Effect.

The ListView object is very simple also:

                <ListView
                    Grid.Row="1"
                    d:ItemsSource="{d:SampleData ItemCount=5}"
                    ItemTemplate="{StaticResource ClienteListItemTemplate}"
                    ItemsSource="{Binding Clientes, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=False}"
                    SelectedItem="{Binding ClienteSelecionado, Mode=TwoWay}">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>

The C# code:

ItemsSource:

public ObservableCollection<ClienteViewModel> Clientes { get; } = new ObservableCollection<ClienteViewModel>();
        

ClienteViewModel:

    public class ClienteViewModel : ObservableValidator
    {
        ...
        private int _orcamentosPendentes;
        public int OrcamentosPendentes
        {
            get { return _orcamentosPendentes; }
            set { SetProperty(ref _orcamentosPendentes, value); }
        }
    }

I tried changing the Bindings with and without the UpdateSourceTrigger=PropertyChanged inside the DataTemplate and also in the ItemsSource of the ListView and no changes either.

Is there something I'm forgetting? Something wrong in the code? Should I use any other option than ListView?

Any help is appreciated.

0

There are 0 answers