我正在尝试刷新我的WPF应用程序中的列表框,但它似乎无法正常工作。
如果我在添加值后重新加载应用程序,则会添加值。
我有一个名为' listboxData':
的变量和属性 ObservableCollection<ITimeLineDataItem> listboxData = new ObservableCollection<ITimeLineDataItem>();
public ObservableCollection<ITimeLineDataItem> ListBoxData
{
get
{
return listboxData;
}
}
我已将Listbox的值绑定到此属性:
<ListBox x:Name="ListSrc" ItemsSource="{Binding Path=ListBoxData}" dd:DragDrop.IsDragSource="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="0">
当用户添加一个值时,会调用此代码:
public void RefreshListbox()
{
listboxData.Clear();
foreach (podiaPublish.Marker pMarker in Global.gChapter.MarkerList)
{
foreach (podiaPublish.Content pContent in pMarker.ContentList)
{
//FOR SLIDES THAT HAVE JUST BEEN IMPORTED, THERE WILL BE NO FILES ON THE SERVER (SO NO CHECKSUMS AVAILABLE)
if (File.Exists(Global.TempFolder + "\\" + (string.IsNullOrEmpty(pContent.AlternateMarkup) ? pContent.Markup : pContent.AlternateMarkup)))
{
//KLUDGE TO RESIZE THE THUMBNAILS
System.IO.FileStream fs = new System.IO.FileStream(Global.TempFolder + "\\" + (string.IsNullOrEmpty(pContent.AlternateMarkup) ? pContent.Markup : pContent.AlternateMarkup), FileMode.Open, FileAccess.Read);
Bitmap bm = new Bitmap(fs);
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
bm.Dispose();
fs.Close();
if (pContent.Markup.Contains(".png"))
{
var brush = new ImageBrush(bitmapSource);
var lb1 = new TempDataType()
{
Name = pContent.Markup,
BackgroundImage = brush
};
listboxData.Add(lb1);
}
}
}
}
}
所以我先清除&#39; listboxData&#39;然后你可以在foreach循环中看到我重新添加新值。调试时返回正确的值。
编辑:
列表框的属性绑定在:
中 <DataTemplate DataType="{x:Type tt:TempDataType}">
<Border BorderThickness="1"
BorderBrush="Black"
Background="{Binding BackgroundImage}"
CornerRadius="3"
Height="120">
<StackPanel Orientation="Vertical">
<Image Source="{Binding BackgroundImage}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
</DataTemplate>
TempDataType类的结构如下:
public class TempDataType : ITimeLineDataItem, INotifyPropertyChanged
{
public TimeSpan? StartTime { get; set; }
public TimeSpan? EndTime { get; set; }
public ImageBrush BackgroundImage { get; set; }
public Boolean TimelineViewExpanded { get; set; }
public String Name { get; set; }
}
答案 0 :(得分:0)
我相信您正在使用MVVM模式,但如果您不使用MVVM,则无法以任何方式查看代码中的属性更改事件(INotifyPropertyChanged),然后直接使用Listbox对象在列表中添加项目:
public ObservableCollection<ITimeLineDataItem> ListBoxData
{
get
{
return this.listboxData;
}
set
{
this.listboxData = value;
this.RaisePropertyChanged(() => this.ListBoxData);
}
}
答案 1 :(得分:0)
请参阅此代码,我已在viewmodel中实现了该接口:
class YourViewModel:INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
//method to fire the property changed event ...
void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
//To raise the changes in property ...
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
this.OnPropertyChanged(propertyName);
}
#endregion
}