简体   繁体   English

Silverlight:难以与可见度绑定

[英]Silverlight: Difficulty with binding to visiblity

I have two elements: a listbox and a "this list is empty" message. 我有两个元素:一个列表框和一个“此列表为空”消息。 I'd like to bind their visibility to the list box's ItemsSource being empty. 我想将其可见性绑定到列表框的ItemsSource为空。 However, I'm not sure how to do this: 但是,我不确定如何执行此操作:

        <TextBlock Text="No favorite searches yet. Add some by searching, then clicking 'Add to Favorites'" 
                   Padding="10,0" 
                   VerticalAlignment="Center"
                   Visibility="{Binding Path=FavoriteFilters.IsEmpty, Converter={StaticResource visibilityConverter}}"
                   />

        <ListBox ItemsSource="FavoriteFilters" 
                 x:Name="favoriteFiltersList" 
                 Visibility="{Binding Path=FavoriteFilters.IsEmpty, Converter={StaticResource visibilityConverter}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <my:FavoriteFilterLink />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

They are both visible, although the ItemsSource is empty. 它们都是可见的,尽管ItemsSource为空。 Also, I'm not sure how to invert the condition for the ListBox . 另外,我不确定如何反转ListBox的条件。

The visibility converter: 可见性转换器:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool?)
        {
            if (string.IsNullOrEmpty((string)parameter))
            {
                return (value as bool?).Value ? Visibility.Visible : Visibility.Collapsed;
            } else
            {
                return (value as bool?).Value ? Visibility.Collapsed : Visibility.Visible;
            }
        }
        throw new ArgumentException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You might be better off writing two converters, one for the ListBox the other for the TextBlock. 您最好编写两个转换器,一个用于ListBox,另一个用于TextBlock。 The logic of each would be simpler and you wouldn't need to pass a parameter to get the correct result. 每种方法的逻辑都将更简单,并且您无需传递参数即可获得正确的结果。

While it might not be as elegant as a single converter solution it will be far more maintainable. 尽管它可能不如单个转换器解决方案那么优雅,但它将更具可维护性。

If you really want to pass a parameter then you need to use the ConverterParameter . 如果您确实要传递参数,则需要使用ConverterParameter

There's an example here but I'm not 100% sure it'll meet your requirements. 这里有一个例子但我不是100%确定它会满足您的要求。 The simplified XAML syntax is: 简化的XAML语法为:

<TextBlock Visibility="{Binding FavoriteFilters.IsEmpty,
     Converter={StaticResource visibilityConverter}, ConverterParameter=false}"/>

<ListBox Visibility="{Binding FavoriteFilters.IsEmpty,
     Converter={StaticResource visibilityConverter}, ConverterParameter=true}"/>

Then in your converter (simplified): 然后在您的转换器中(简体):

bool show = (bool)value;
bool visible = (bool)parameter;
if (visible)
{
    return show ? Visibility.Visible : Visibility.Collapsed;
}
else
{
    return show ? Visibility.Collapsed : Visibility.Visible;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM