C# 过滤视图模型中的数据后,它';它不显示/刷新视图
我有两种类型的站点列表,我正在通过下面的代码过滤内部视图模型C# 过滤视图模型中的数据后,它';它不显示/刷新视图,c#,wpf,c#-4.0,mvvm,combobox,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_Ec3
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"));
}
public void FilterSite()
{
如果(选择editem.Contains(“EC350”))
listofsites=new ObservableCollection(listofsites.Where(p=>Convert.ToString(p.DeviceType)==“胶束Ec350”);
如果(SelectedItem.Contains(“胶束”))
listofsites=new ObservableCollection(listofsites.Where(p=>Convert.ToString(p.DeviceType)=“胶束”);
else if(SelectedItem.Contains(“Mini-Max”))
listofsites=新的ObservableCollection(listofsites.Where(p=>Convert.ToString(p.DeviceType)=“Mini-Max”);
}
现在,为了在站点列表中获得自动更新,我正在属性设置器内部实现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");
}
}
公共类SiteMainUC\u VM:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
私有ObservableCollection listofsites=null;
公共可观测收集站点列表
{
得到
{
返回站点列表;
}
设置
{
网站列表=价值;
房地产变更(“现场清单”);
}
}
在组合框选择值之后,通过调试,我看到了过滤后的值,但视图没有显示。现在对于绑定,我尝试了单向/双向绑定,但都不起作用。下面是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" >
您忘记将ComboBox的ItemsSource绑定到基础集合。您的XAML应该如下所示:
<ComboBox x:Name="cmbSiteSearch" Height="18" Width="18"
IsReadOnly="True" FontFamily="Arial"
Text="{Binding SearchFilter, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Listofsites}"/>
公共无效筛选器站点()
{
listofsites=新的可观测集合(参数);
如果(选择editem.Contains(“EC350”))
Listofsites=new ObservableCollection(Listofsites.Where(p=>Convert.ToString(p.DeviceType)==“胶束Ec350”);
如果(SelectedItem.Contains(“胶束”))
Listofsites=new ObservableCollection(Listofsites.Where(p=>Convert.ToString(p.DeviceType)=“胶束”);
else if(SelectedItem.Contains(“Mini-Max”))
Listofsites=新的ObservableCollection(Listofsites.Where(p=>Convert.ToString(p.DeviceType)=“Mini-Max”);
}
1) 在filter方法内部添加私有变量以将其与可观察集合绑定,否则一旦您过滤您的值,它的值将为null,并且通过第二次单击,您将不会通过过滤获得任何值更改
2) 有时在这里指定私有变量listofsites不会给您提供所需的结果,并且通过view与viewmodel进行通信时会出现问题。虽然这是一种糟糕的编码方式,但使用直接属性名而不是变量(即;listofsites
3) 我也曾多次遇到过类似的视图刷新问题。要获得更好的样式,您应该选择MessageBus体系结构样式。发布/订阅样式可用于与vm到vm或vm到视图的通信
下面的链接
希望这有帮助。对于可观察的集合,您不必实现OnPropertyChanged,相反,您应该使用collection.Clear()。并将所有新项添加到集合中,如collection.add(filteredItem)。尝试了添加(filteredItem)。但没有获得。您是否也在子视图模型上执行了相同的通知操作,即SiteDetailsNo,have not as is api[dll]由客户端提供,此应用程序的所有数据都来自该客户端。因为您说过我将尝试.Thanx获取提示。listbox[all site list]已经包含
ItemsSource=“{Binding Listofsites}”
,这就是为什么如果我把同样的东西放在combobox中,它会给出错误。如果没有完整的上下文,仍然很难理解这个问题。你确定你正确设置了DataContext吗?在FilterSite()方法中,你将一个值赋给listofsites字段而不是listofsites属性,这不会导致OnPropertyChanged调用。
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"));
}