Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF C#刷新/重新绑定列表框_C#_Wpf_Listbox - Fatal编程技术网

WPF C#刷新/重新绑定列表框

WPF C#刷新/重新绑定列表框,c#,wpf,listbox,C#,Wpf,Listbox,我正在尝试刷新WPF应用程序中的列表框,但它似乎不起作用 如果我在添加值后重新加载应用程序,则会添加值 我有一个变量和一个名为“listboxData”的属性: ObservableCollection<ITimeLineDataItem> listboxData = new ObservableCollection<ITimeLineDataItem>(); public ObservableCollection<ITimeLineDataItem

我正在尝试刷新WPF应用程序中的列表框,但它似乎不起作用

如果我在添加值后重新加载应用程序,则会添加值

我有一个变量和一个名为“listboxData”的属性:

    ObservableCollection<ITimeLineDataItem> listboxData = new ObservableCollection<ITimeLineDataItem>();
    public ObservableCollection<ITimeLineDataItem> ListBoxData
    {
        get
        {
            return listboxData;
        }        
    }
因此,我首先清除“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>

我相信您正在使用MVVM模式,但在代码中无法以任何方式看到属性更改事件(INotifyPropertyChanged)。如果您未使用MVVM,请直接使用Listbox对象在列表中添加项目:

    public ObservableCollection<ITimeLineDataItem> ListBoxData
    {
        get
        {
            return this.listboxData;
        }
        set
        {
                this.listboxData = value;
                this.RaisePropertyChanged(() => this.ListBoxData);

        }
    }
public-observeCollection ListBoxData
{
得到
{
返回此.listboxData;
}
设置
{
this.listboxData=值;
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
}
class YourViewModel:INotifyPropertyChanged
{
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
//方法激发属性已更改事件。。。
void OnPropertyChanged(字符串propertyName)
{
var handler=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
//要提高属性的更改。。。
受保护的void RaisePropertyChanged(表达式属性Expression)
{
var propertyName=PropertySupport.ExtractPropertyName(propertyExpression);
此.OnPropertyChanged(propertyName);
}
#端区
}

name和backgroundImage是否会抛出已更改的属性?@Muds不,我不会这样做。我只是有一个隐藏的代码和一个绑定到它的属性。你能给我们展示TempDataType.Name的代码吗?@Muds我添加了一些代码,展示了背景图像和名称如何绑定到列表框的属性,以及TempDataType类的结构。你需要在TempDataType中实现INotifyPropertyCHanged,然后提升绑定属性的属性已更改感谢您的回答。我似乎找不到RaisePropertyChanged事件的用法。。。你知道这是什么吗?我没有用ViewModel来做这个?我需要能够在不使用ViewModel的情况下进行刷新。在这种情况下,您需要保留/访问ListSrc DDL的实例,并直接刷新item source one get new list box数据。我正在刷新listboxData列表,但问题是页面上的ListBox没有刷新。Ben,在您不触发PropertyChanged事件之前,它不会自动更新目标对象,但在您的情况下,您没有使用MVVM,因此不会应用它。要修复此问题,您需要编写以下代码**ListSrc.ItemsSource=ListBoxData;**在RefreshListbox()方法的末尾。
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; }
}
    public ObservableCollection<ITimeLineDataItem> ListBoxData
    {
        get
        {
            return this.listboxData;
        }
        set
        {
                this.listboxData = value;
                this.RaisePropertyChanged(() => this.ListBoxData);

        }
    }
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
}