ListBox在非调试中不更新

时间:2010-02-22 17:23:42

标签: c# wpf data-binding listbox

有没有人知道为什么我的ListBox在非Visual Studio Debug版本中运行时不会填充?在调试版本(F5)中运行时,它完全正常,没有任何断点,但是当我在非调试(Ctrl + F5)中运行时,它不会被填充。

我甚至不知道从哪里开始,因为谷歌显示没有用,我不能使用调试器,因为它在调试时工作正常。

public partial class ErrorLog: Window
    {
        /// <summary>
        /// Collection of all errors
        /// </summary>
        public static SafeObservable<ErrorMessage> ErrorList
        {
            get {
                return ErrorLog.errorList; 
            }
            set
            {
                ErrorLog.errorList = value;
            }
        } private static SafeObservable<ErrorMessage> errorList = new SafeObservable<ErrorMessage>();

        /// <summary>
        /// Default constrcutor
        /// </summary>
        public ErrorLog()
        {
            InitializeComponent();
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            //ErrorLog.ErrorList.Clear();
        }
    }

SafeObservable类只是普通ObservableColleciton类的扩展,它使用调度程序在必要时调用来从不同的线程更新UI。我在整个项目中使用这个系列在大约12个不同的地方,它的工作非常好。但在这个单一实例中却没有。

Link to SafeObservable is here

XAML代码位于

之下
<Window x:Class="Dashboard.ErrorLog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Dashboard"
    Title="ErrorLog" Height="448" Width="822" WindowStyle="ToolWindow">
    <Grid Background="#222222">
        <ListBox Name="Error_listBox" ItemsSource="{Binding Source={x:Static local:ErrorLog.ErrorList}}" Margin="12,12,12,41" IsSynchronizedWithCurrentItem="True" />
        <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12" Name="Clear" VerticalAlignment="Bottom" Width="75" Click="Clear_Click">Clear</Button>
    </Grid>
</Window>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

多奇怪。

如果我只是从主窗口调用任何函数,它就可以了。

我在初始化主窗口后添加了ErrorLog.ErrorList.Clear(),现在工作正常。任何人都有任何想法,为什么这样的?这个集合是静态的吗?

我不喜欢随机代码,只是为了使某些东西工作......必须有一个更好的方法来做它。

感谢奥斯汀的建议