XAML:
<ListBox x:Name="RegionList" Loaded="RegionList_Loaded" Grid.Row="1" Foreground="Black" Margin="0,0,0,10" SelectionChanged="RegionList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<selectors:RegionDataTemplateSelector Content="{Binding}">
<selectors:RegionDataTemplateSelector.ItemTemplateFirstLayer>
<DataTemplate>
<TextBlock Text="{Binding DisplayedName}" Visibility="{Binding VisibilityOption, Mode=TwoWay}" FontSize="24" Margin="5,5,5,5"></TextBlock>
</DataTemplate>
</selectors:RegionDataTemplateSelector.ItemTemplateFirstLayer>
<selectors:RegionDataTemplateSelector.ItemTemplateSecondLayer>
<DataTemplate>
<TextBlock Text="{Binding DisplayedName}" Visibility="{Binding VisibilityOption, Mode=TwoWay}" Margin="25,5,5,5" TextWrapping="Wrap" Width="350" FontSize="24"/>
</DataTemplate>
</selectors:RegionDataTemplateSelector.ItemTemplateSecondLayer>
<selectors:RegionDataTemplateSelector.ItemTemplateThirdLayer>
<DataTemplate>
<TextBlock Text="{Binding DisplayedName}" Visibility="{Binding VisibilityOption, Mode=TwoWay}" Margin="45,5,5,5" TextWrapping="Wrap" Width="350" FontSize="24"/>
</DataTemplate>
</selectors:RegionDataTemplateSelector.ItemTemplateThirdLayer>
</selectors:RegionDataTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#:
private void FilterBtn_Click(object sender, RoutedEventArgs e)
{
var allRegions = (RegionList.ItemsSource as ObservableCollection<RegionTree>);
foreach (var region in allRegions)
{
if (region.DisplayedName.IndexOf(RegionFilter.Text) >= 0)
{
region.VisibilityOption = Visibility.Visible;
}
else
{
region.VisibilityOption = Visibility.Collapsed;
}
}
RegionList.ItemsSource = allRegions;
}
当我的列表第一次加载时,所有项目都有VisibilityOption = Visibility.Visible。当我按下按钮时,他们中的一些人从那一刻起就有了VisibilityOption = Visibility.Collapsed。但我仍然可以在UI中看到它们。出了什么问题?
修改
RegionTree:
public class RegionTree : BindableBase
{
public bool IsFirstLayer { get; set; }
public bool IsSecondLayer { get; set; }
public bool IsThirdLayer { get; set; }
public Visibility VisibilityOption { get; set; }
public string Name;
public string Code;
public string RUName;
public string SWNE;
public string DisplayedName
{
get
{
if (Thread.CurrentThread.CurrentUICulture.ToString() == "ru-RU" && RUName != null)
{
return RUName;
}
else { return Name; }
}
}
}
答案 0 :(得分:1)
更改属性时必须引发PropertyChanged事件,否则Binding将不会更新。您的BindingBase
类实现了INotifyPropertyChanged
,您可以/应该将其用于视图的通知
(http://msdn.microsoft.com/de-de/library/vstudio/system.componentmodel.inotifypropertychanged)
你的财产应该是这样的:
public Visibility VisibilityOption
{
get { return _visibilityOption; }
set
{
if (_visibilityOption != value)
{
_visibilityOption = value;
OnPropertyChanged("VisibilityOption");
}
}
}
答案 1 :(得分:1)
public Visibility VisibilityOption { get; set; }
这不是添加属性的正确方法,您必须使用OnPropertyChanged("VisibilityOption")
才能更新set
中的用户界面。只需修改该属性即可。您将完成。