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

时间:2016-05-20 09:22:42

标签: c# wpf c#-4.0 mvvm combobox

我有两种类型的网站列表,我在视图模型中通过以下代码过滤

  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"));


    }

现在要在 listofsites 中获取自动更新,我正在设置属性中 InotifyPropertyChanged OnPropertyChanged

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" >

2 个答案:

答案 0 :(得分:1)

 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进行通信时会出现问题。虽然这是一种糟糕的编码风格,但使用直接属性名称而不是变量是快捷和有用的,I,e;的 Listofsites

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

以下https://msdn.microsoft.com/en-us/library/ff647328.aspx

链接

希望这有帮助。

答案 1 :(得分:0)

您忘记将ComboBox的ItemsSource绑定到基础集合。你的XAML应该是这样的:

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