从ItemsSource

时间:2018-02-28 18:06:10

标签: c# wpf xaml datatrigger

我有一个Listbox,我在xaml中有一个DataTrigger,用于检查ListBox中是否有项目然后隐藏它(请参阅HasItems DataTrigger)。在我的应用程序的某些时候,我正在清除ItemsSource中的项目,即FilteredVolumesList(请参阅DeSelectAssays方法),假设ListBox将被隐藏,因为ItemsSource中没有项目。

但问题是DataTrigger似乎不起作用而且ListBox没有被折叠。请帮忙。我不想为可见性创建bool属性并将其设置为false。请注意我在xaml ListBox CLear Items Xamly

中做了类似的事情

这是我的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>

我正在从

背后的代码中清除这样的项目
 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();
        }

这是viewmodel中的FilteredVolumesList

private List<Volume> _filteredVolumesList;
        public List<Volume> FilteredVolumesList
        {
            get
            {
                return _filteredVolumesList;
            }
            set
            {
                _filteredVolumesList = value;
                OnPropertyChanged("FilteredVolumesList");
            }
        }

0 个答案:

没有答案