开发者

Silverlight: Difficult with Data Binding

Apparently I don't understand something about binding ListBox controls to data.

Here is a ListBox that binds to a property in the code-behind:

        <ListBox ItemsSource="FavoriteFilters" 
                 x:Name="favoriteFiltersList" 
                 Visibility="{Binding FavoriteFilters.IsEmpty, Converter={StaticResource visibilityConverter}}">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <my:FavoriteFilterLink />
                </DataTemplate>
            </ListBox.ItemTemplate>开发者_高级运维;
        </ListBox>

When I do this, the ListBox appears with a single element populated with fallback values, even though the items source is empty. This happens no matter what I set ItemsSource to, like ItemsSource="TotallyInvalidProperty". However, if ItemsSource is empty, the ListBox disappears.

The code behind:

    public ObservableCollection<FavoriteFilter> FavoriteFilters
    {
        get
        {
            return PlumData.FavoriteFilters;
        }
    }

PlumData:

    private static ObservableCollection<FavoriteFilter> _favoriteFilters = new ObservableCollection<FavoriteFilter>();
    public static ObservableCollection<FavoriteFilter> FavoriteFilters
    {
        get
        {
            return _favoriteFilters;
        }
    }

I don't understand why this isn't working. However, when I do it in the code-behind, it works fine:

    void BottomFavoritesBar_Loaded(object sender, RoutedEventArgs e)
    {
        favoriteFiltersList.ItemsSource = FavoriteFilters;
    }

What am I doing wrong? I'm using SL4.


off the top of my head, don't you need to set the binding like this:

<ListBox ItemsSource="{Binding FavoriteFilters}" 
             x:Name="favoriteFiltersList" 
             Visibility="{Binding FavoriteFilters.IsEmpty, Converter={StaticResource visibilityConverter}}">

note the Binding keyword.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜