我有兩種類型的網站列表,我用下面的代碼是過濾視圖模型裏面現在過濾的視圖模型的數據後,它沒有顯示/刷新視圖
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裏面的屬性setter
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" >
使用Observable集合,您不必實現OnPropertyChanged,而應該使用Collection.Clear()。並將所有新項目添加到Collection.Add(filteredItem) –
嘗試。但沒有得到。 – user3165200
您是否在子視圖模型上也做了同樣的通知事項,即SiteDetails –