我有一個列表框,我已經將它的ItemsSource和SelectedIndex綁定到ViewModel中的對象。我還使用擴展程序自動滾動到所選項目。當我創建列表時,有一些初始數據,然後當我在列表中移動時,會添加更多項目。初始數據顯示正常,我可以按照我的預期滾動瀏覽它。問題是,一旦我添加更多的項目,新項目不會顯示在列表框中。當我移動到新項目時,列表框實際上會停止滾動到所選項目,但是當我移回時會再次開始。當我看着Snoop中的ItemsSource時,它顯示了所有的項目,但它們沒有顯示。唯一顯示的項目是原始項目。所以我想我的問題是爲什麼會發生這種情況?列表框不顯示新項目
這裏是我使用列表框
<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>
這裏的XAML是我不知道還有什麼其他信息將是有益的擴展
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]);
}
}
}
的代碼,所以如果有什麼你需要看到讓我知道,我會添加它。
列表框的SelectedIndex勢必FocusedRow
private uint focusedRow;
public uint FocusedRow
{
get
{ return focusedRow; }
set
{
if (value == focusedRow)
{ return; }
focusedRow = value;
base.RaisePropertyChanged("FocusedRow");
}
}
ItemsSource綁定到項目
private ObservableCollection<DisplayList> items;
public ObservableCollection<DisplayList> Items
{
get
{ return items; }
set
{
if (value == items)
{ return; }
items = value;
base.RaisePropertyChanged("Items");
}
}
什麼是「物品」? – JeffRSon 2014-10-27 13:29:09
問題出在你的RowHeight和RowWidht上嗎?他們是你的視圖模型成員嗎?有正確的值? – whoisthis 2014-10-27 13:45:10
@whoisthis這就是問題所在。新項目的行高和寬度都是0.如果你讓它成爲答案,我會接受它。 – scott 2014-10-28 14:08:05