在视图模型中过滤数据后,它不显示/刷新视图



我有两种类型的站点列表,我通过以下代码在视图模型内过滤它们

  public void FilterSite()
    {
        if (SelectedItem.Contains("EC350"))
            listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "MiCell_Ec350"));
        else if (SelectedItem.Contains("MiCell"))
            listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "MiCell"));
        else if (SelectedItem.Contains("Mini-Max"))
            listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "Mini-Max"));

    }

现在为了在站点列表中获取自动更新,我正在属性的setter中实现InotifyPropertyChangedOnPropertyChanged

public class SiteMainUC_VM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<SiteDetails> listofsites = null;
    public ObservableCollection<SiteDetails> Listofsites
    {
        get
        {
            return listofsites;
        }
        set
        {
            listofsites = value;
            OnPropertyChanged("Listofsites");
        }
    }

组合框选择值后,通过调试,我看到了过滤后的值,但没有显示视图。现在对于绑定,我已经尝试了单向/双向两种方式,但不起作用。下面是 xaml 代码-

<ComboBox Name="cmbSiteSearch" SelectedValue="{Binding SelectedItem, Mode=TwoWay}" Text="{Binding SearchFilter,UpdateSourceTrigger=PropertyChanged}"  Height="18" Width="18" IsReadOnly="True" FontFamily="Arial"   >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBox.Background>
                    <ImageBrush ImageSource="/MasterLink;component/Resources/i_filter.png"    />
                </ComboBox.Background>
                <ComboBoxItem Content="All" Height="34" Width="190" FontFamily="Arial" FontSize="12" />
                <ComboBoxItem Content="EC350" Height="34" Width="190" FontFamily="Arial" FontSize="12"/>
                <ComboBoxItem Content="Mini-Max" Height="34" Width="190" FontFamily="Arial" FontSize="12"/>
            </ComboBox>

现在对于我有的站点列表列表框代码

<ListBox    ItemsSource="{Binding Listofsites}"  SelectedItem="{Binding Path=Selectedsites, Mode=TwoWay,NotifyOnSourceUpdated=True}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Height="600" 
          SelectionChanged="ListBox_SelectionChanged" >
 public void FilterSite()
    {
     listofsites = new ObservableCollection<SiteDetails>(parameters);
if (SelectedItem.Contains("EC350"))
            Listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "MiCell_Ec350"));
        else if (SelectedItem.Contains("MiCell"))
            Listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "MiCell"));
        else if (SelectedItem.Contains("Mini-Max"))
            Listofsites = new ObservableCollection<SiteDetails>(listofsites.Where(p => Convert.ToString(p.DeviceType) == "Mini-Max"));

    }

1)在过滤器方法中添加私有变量以将其与可观察集合绑定,否则一旦过滤了值,它的值将为 null,并且通过第二次单击您将不会通过过滤获得任何值更改。

2)有时在这里分配私有变量 listofsites 不会给你想要的结果,并且通过视图到 viewmodel 进行通信时会出现问题。虽然这是一种糟糕的编码风格,但使用直接属性名称而不是变量是快捷方式和有用的,即;网站列表

3)我也多次遇到过这种类似的视图刷新问题。为了获得更好的风格,您应该选择MessageBus架构风格。可以实现发布/订阅样式,以便与虚拟机到虚拟机或虚拟机进行通信以查看。

链接 belowhttps://msdn.microsoft.com/en-us/library/ff647328.aspx

希望这有帮助。

您忘记将 ComboBox 的 ItemsSource 绑定到基础集合。XAML 应如下所示:

<ComboBox x:Name="cmbSiteSearch" Height="18" Width="18" 
          IsReadOnly="True" FontFamily="Arial"
          Text="{Binding SearchFilter, UpdateSourceTrigger=PropertyChanged}"
          ItemsSource="{Binding Listofsites}"/>

最新更新