0

I have a Listbox and I have a DataTrigger in xaml to check if there are no items in the ListBox then Hide it (Please see the HasItems DataTrigger). At some point in my application I am Clearing the items from the ItemsSource i.e FilteredVolumesList (Please see the DeSelectAssays method) assuming the ListBox would be hidden because there are no Items In the ItemsSource.

But the problem is the DataTrigger dosen't seem to work and the ListBox is not being collapsed. Please help. I dont want to create a bool property for visibility and keep setting to false. Please note I am doing something like this in xaml ListBox CLear Items Xamly

Here is my xaml

<ListBox
            Grid.Row="1"
            x:Name="LstVolumes"
            HorizontalAlignment="Center"
            Margin="0,90,0,0"
            VerticalAlignment="Top"
            ItemsSource="{Binding FilteredVolumesList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ScrollViewer.VerticalScrollBarVisibility="Disabled"
            SelectionChanged="LstVolumes_SelectionChanged"
            BorderThickness="0">
            <ListBox.Style>
                <Style
                    TargetType="{x:Type ListBox}"
                    BasedOn="{StaticResource ListBoxDarkBackgroundStyle}">
                    <Setter
                        Property="Template"
                        Value="{StaticResource ListBoxVolumesTemplate}"></Setter>
                    <Style.Triggers>
                        <DataTrigger
                            Binding="{Binding HasItems,RelativeSource={RelativeSource Self},UpdateSourceTrigger=PropertyChanged}"
                            Value="False">
                            <Setter
                                Property="Visibility"
                                Value="Collapsed"></Setter>
                        </DataTrigger>

                        <DataTrigger
                            Binding="{Binding DataContext.IsVolumeRepeaterVisible,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}"
                            Value="False">
                            <Setter
                                Property="Template"
                                Value="{StaticResource ListBoxNoRepeaterTemplate}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Style>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel
                        Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button
                        x:Name="VolumesButton"
                        Style="{StaticResource ButtonLightTextGradientDarkBackgroundStyle}"
                        Margin="{StaticResource AllControlsMargin}"
                        Click="VolumesButton_Click">
                        <Button.Content>
                            <StackPanel
                                x:Name="stkInnerChildPanel"
                                Orientation="Vertical"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center">
                                <TextBlock
                                    Style="{StaticResource TextBlockLightTextStyle}"
                                    x:Name="TblVolume"
                                    Text="{Binding volume_display_value,StringFormat={}{0} µg}"></TextBlock>
                            </StackPanel>
                        </Button.Content>
                    </Button>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

And I am clearing the items like this from the code behind

 private ReadJsonAssayViewModel AssayViewModel = null;

        public RunSetUpMainContentUserControl()
        {
            InitializeComponent();
            AssayViewModel = ReadJsonAssayViewModel.ReadJsonAssayViewModelInstance;
            this.DataContext = AssayViewModel;
            FindAssociatedChildrenInstance = FindChildrenAndAssociatedControlsHelper.FindAssociateChildrenInstance;
            Messenger.Default.Register<NotificationMessage>(this, DeSelectAssays);
        }

        private void DeSelectAssays(NotificationMessage obj)
        {
            for (int i = 0; i < LstAssays.Items.Count; i++)
            {
                ToggleButton btn = FindAssociatedChildrenInstance.AssociatedPanel(LstAssays, "AssaysButton", i, TargetType.ListAssays, typeof(ToggleButton)) as ToggleButton;
                btn.IsChecked = false;
            }
            LstAssays.SelectedIndex = -1;
            AssayViewModel.FilteredVolumesList.Clear();
            AssayViewModel.IsStartRunEnabled = false;
            this.Focus();
        }

Here is the FilteredVolumesList in the viewmodel

private List<Volume> _filteredVolumesList;
        public List<Volume> FilteredVolumesList
        {
            get
            {
                return _filteredVolumesList;
            }
            set
            {
                _filteredVolumesList = value;
                OnPropertyChanged("FilteredVolumesList");
            }
        }
Nikhil ANS
  • 53
  • 1
  • 8

0 Answers0