[英]WPF C# Refresh/Rebind Listbox
我正在嘗試刷新WPF應用程序中的列表框,但它似乎不起作用。
如果我在添加值后重新加載應用程序,則確實會添加值。
我有一個變量和一個名為“ listboxData”的屬性:
ObservableCollection<ITimeLineDataItem> listboxData = new ObservableCollection<ITimeLineDataItem>();
public ObservableCollection<ITimeLineDataItem> ListBoxData
{
get
{
return listboxData;
}
}
我已將列表框的值綁定到此屬性:
<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);
}
}
}
}
}
因此,我首先清除了“ listboxData”,然后如您在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; }
}
我相信您使用的是MVVM模式,但是如果您未使用MVVM,則無法以任何方式在代碼中看到屬性更改事件(INotifyPropertyChanged),然后直接使用Listbox對象在列表中添加項目:
public ObservableCollection<ITimeLineDataItem> ListBoxData
{
get
{
return this.listboxData;
}
set
{
this.listboxData = value;
this.RaisePropertyChanged(() => this.ListBoxData);
}
}
我已經在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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.