我有 Listbox 控件,当用户点击按钮时,他们可以向控件添加项目。
我遇到的问题是 Listbox 控件本身没有刷新并显示用户添加的新值。我调试了代码,数据源正在使用新值进行更新。
有人有什么想法吗?
C#:
ObservableCollection<ITimeLineDataItem> listboxData = new ObservableCollection<ITimeLineDataItem>();
public ObservableCollection<ITimeLineDataItem> ListBoxData
{
get
{
return listboxData;
}
}
public Live()
{
InitializeComponent();
this.DataContext = this;
}
public void RefreshListbox()
{
ListSrc.ItemsSource = null;
//THE CODE GOES HERE WHERE I UPDATE THE DATASOURCE.
ListSrc.ItemsSource = ListBoxData;
}
XAML:
<Border BorderBrush="#d6786a" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="2" Grid.Column="15" Grid.Row="5" Grid.ColumnSpan="3">
<ListBox x:Name="ListSrc" Background="#ececec" ItemsSource="{Binding Path=ListBoxData}" dd:DragDrop.IsDragSource="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="0">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="10" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="4" Margin="15"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
</ListBox>
</Border>
答案 0 :(得分:1)
您不需要刷新。如果新项目正确添加到源,它应该在列表中可见。但是,您可以使用按钮单击事件代码更新帖子吗?