Silverlight:绑定可见性的困难

发布于 2024-10-05 02:50:53 字数 1786 浏览 1 评论 0原文

我有两个元素:一个列表框和“此列表为空”消息。我想将它们的可见性绑定到列表框的 ItemsSource 为空。但是,我不确定如何执行此操作:

        <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>

尽管 ItemsSource 为空,但它们都是可见的。另外,我不确定如何反转 ListBox 的条件。

可见性转换器:

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();
    }
}

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. 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. Also, I'm not sure how to invert the condition for the 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-10-12 02:50:53

您最好编写两个转换器,一个用于 ListBox,另一个用于 TextBlock。每个的逻辑都会更简单,您不需要传递参数来获得正确的结果。

虽然它可能不如单个转换器解决方案那么优雅,但它的可维护性要高得多。

如果您确实想传递参数,那么您需要使用ConverterParameter。

有一个示例这里< /a> 但我不能 100% 确定它会满足您的要求。简化的 XAML 语法为:

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

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

然后在转换器中(简化):

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

You might be better off writing two converters, one for the ListBox the other for the 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.

There's an example here but I'm not 100% sure it'll meet your requirements. The simplified XAML syntax is:

<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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文