列表框不显示新项目

时间:2014-10-27 13:27:38

标签: wpf mvvm

我有一个列表框,我将其ItemsSource和SelectedIndex绑定到ViewModel中的对象。我也使用扩展器自动滚动到所选项目。当我创建列表时,有一些初始数据,然后当我在列表中移动时,会添加更多项目。初始数据显示正常,我可以按照我的预期滚动它。问题是,一旦我添加更多项目,新项目就不会显示在列表框中。当我移动到新项目时,列表框会停止实际滚动到所选项目,但在我向后移动时会再次启动。当我在Snoop中查看ItemsSource时,它会显示所有项目,但不显示它们。显示的唯一项目是原始项目。所以我想我的问题是为什么会发生这种情况?

这是我用于列表框的xaml

<ListBox ItemsSource="{Binding Path=Items}"
                     Width="{Binding Path=Size.SizeW}"
                     Height="{Binding Path=Size.SizeH}"
                     SelectedIndex="{Binding Path=FocusedRow, Mode=OneWay}"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                     ScrollViewer.VerticalScrollBarVisibility="Hidden"
                     IsSynchronizedWithCurrentItem="True"
                     extenders:ListBoxExtenders.AutoScrollToCurrentItem="True">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Height"
                    Value="{Binding Path=RowHeight}" />
            <Setter Property="Width"
                    Value="{Binding Path=RowWidth}" />
            <Setter Property="Margin"
                    Value="{Binding Path=RowSpacing}" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <m:CanvasItemsControl ItemTemplateSelector="{StaticResource ResourceKey=listViewItemTemplateSelector}"
                                  Visibility="Visible"
                                  ItemsSource="{Binding Path=ListItems}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Height="{Binding Path=RowHeight}"
                                Width="{Binding Path=RowWidth}"
                                ClipToBounds="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </m:CanvasItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是扩展程序的代码

public class ListBoxExtenders : DependencyObject
{
    public static readonly DependencyProperty AutoScrollToCurrentItemProperty = DependencyProperty.RegisterAttached("AutoScrollToCurrentItem", typeof(bool), typeof(ListBoxExtenders), new UIPropertyMetadata(default(bool), OnAutoScrollToCurrentItemChanged));

    public static bool GetAutoScrollToCurrentItem(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoScrollToCurrentItemProperty);
    }

    public static void SetAutoScrollToCurrentItem(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoScrollToCurrentItemProperty, value);
    }

    public static void OnAutoScrollToCurrentItemChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
    {
        var listBox = s as ListBox;
        if (listBox != null)
        {
            var listBoxItems = listBox.Items;
            if (listBoxItems != null)
            {
                var newValue = (bool)e.NewValue;

                var autoScrollToCurrentItemWorker = new EventHandler((s1, e2) => OnAutoScrollToCurrentItem(listBox, listBox.Items.CurrentPosition));

                if (newValue)
                { listBoxItems.CurrentChanged += autoScrollToCurrentItemWorker; }
                else
                { listBoxItems.CurrentChanged -= autoScrollToCurrentItemWorker; }
            }
        }
    }

    public static void OnAutoScrollToCurrentItem(ListBox listBox, int index)
    {
        if (listBox != null && listBox.Items != null && listBox.Items.Count > index && index >= 0)
        {
            listBox.ScrollIntoView(listBox.Items[index]);
        }
    }
}

我不确定其他哪些信息会有所帮助,所以如果还有其他什么需要让我知道,我会添加它。

Listbox SelectedIndex绑定到FocusedRow

private uint focusedRow;
public uint FocusedRow
{
    get
    { return focusedRow; }
    set
    {
        if (value == focusedRow)
        { return; }
        focusedRow = value;
        base.RaisePropertyChanged("FocusedRow");
    }
}

ItemsSource绑定到Items

private ObservableCollection<DisplayList> items;
public ObservableCollection<DisplayList> Items
{
    get
    { return items; }
    set
    {
        if (value == items)
        { return; }
        items = value;
        base.RaisePropertyChanged("Items");
    }
}

2 个答案:

答案 0 :(得分:2)

确保新项目具有有效的RowHeight和RowWidth。这可能是问题,因为在窥探时你可以看到你的数据。

答案 1 :(得分:0)

确保您的&#34;项目&#34;绑定为列表框的ItemsSource是一个ObservableCollection&lt;&gt;而不是一个简单的清单。

它应该工作!