我有一个类型为集合的依赖属性,当它的回调基于计数触发时,我需要设置屏幕上某些控件的可见性。
但是控件始终处于折叠状态。根据代码,一个控件始终可见。
XAML 绑定是
<TextBlock Text="106 search results for 'a'" Margin="5,0,100,0" Visibility="{Binding CountLabelVisibleReverse, Converter={StaticResource VisibilityConverter}}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,90,0"
Visibility="{Binding CountLabelVisible, Converter={StaticResource VisibilityConverter}}">
<TextBlock Text="Sort By" />
<ComboBox Style="{StaticResource ComboBoxStyle1}" Width="100" x:Name="ComboBoxSorting" ItemsSource="{Binding SortBy}" />
</StackPanel>
我的两个属性是
public bool CountLabelVisible { get; set; }
public bool CountLabelVisibleReverse { get; set; }
依赖属性回调
private static void ItemsCollectionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs)
{
var listingUserControl = (obj as ListingUserControl);
var itemsResult = (eventArgs.NewValue as List<ItemsResult>);
if (listingUserControl != null && itemsResult != null)
{
listingUserControl.CountLabelVisible = itemsResult.Count > 0;
listingUserControl.CountLabelVisibleReverse =itemsResult.Count <= 0;
}
}
转换器代码是
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == null)
return (bool)value == false ? Visibility.Collapsed : Visibility.Visible;
return (bool)value ? Visibility.Collapsed : Visibility.Visible;
}