我有一個用戶控件以下元素,作爲默認Grid
的唯一子:爲什麼我的ObservableCollection不可見?
<ListView ItemsSource="{Binding LogCollection}" Name="LogView">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Level}" Header="Level"/>
<GridViewColumn DisplayMemberBinding="{Binding FormattedMessage}" Width="500" Header="Message"/>
<GridViewColumn DisplayMemberBinding="{Binding Exception}" Header="Exception"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
在後面的代碼,我轉運的定製NLOG Target
以下事件。我只是處理它,因爲給我一個例子,總的WPF noob,但它似乎沒問題。
private void EventReceived(LogEventInfo message)
{
Dispatcher.Invoke(() =>
{
if (LogCollection.Count >= 50)
{
LogCollection.RemoveAt(LogCollection.Count - 1);
}
LogCollection.Add(message);
});
}
事件觸發的罰款,並在Add
呼叫到達時,我通過代碼。當我在執行時在XAML視圖中將鼠標移到LogCollection
上時,我看到一個「Count = 1」,此時ListView`仍然幸福地意識不到它應該顯示我一個日誌事件。
OK,OK,背後爲我的用戶控件刪節代碼:
public partial class LoggingControl : UserControl
{
public ObservableCollection<LogEventInfo> LogCollection { get; set; }
public LoggingControl()
{
LogCollection = new ObservableCollection<LogEventInfo>();
InitializeComponent();
foreach (Target target in LogManager.Configuration.AllTargets)
{
var memoryEventTarget = target as MemoryEventTarget;
if (memoryEventTarget != null)
{
memoryEventTarget.EventReceived += EventReceived;
}
}
}
private void EventReceived(LogEventInfo message)
{
Dispatcher.BeginInvoke(new Action(() =>
{
LogCollection.Add(message);
}));
}
}
雙向綁定與此場景沒有任何關係。 – 2014-11-02 08:53:27
@MilanNankov我同意,但仍然給它一個bash無論如何。我在列表中沒有做任何事情,必須將其綁定回集合,它只能讀取它。 – ProfK 2014-11-02 08:55:37
順便說一句,@米蘭南科夫和我的評論只是在另一個背景下,因爲刪除,建議TwoWay評論。 – ProfK 2014-11-02 09:01:00