C# mvvm绑定正在间歇性更新
我在datagrid下面的a页面上有一个计数器,我试图使用它来通知用户datagrid中有多少行,以及需要多少行(共900行)。正在更新有问题的文本框,但当更改每行的计数时,文本框将打开。在将400多行插入到datagrid后,它正在更新 模型属性:C# mvvm绑定正在间歇性更新,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,我在datagrid下面的a页面上有一个计数器,我试图使用它来通知用户datagrid中有多少行,以及需要多少行(共900行)。正在更新有问题的文本框,但当更改每行的计数时,文本框将打开。在将400多行插入到datagrid后,它正在更新 模型属性: public string CountText { get { return countText; } set {
public string CountText
{
get
{
return countText;
}
set
{
countText = value;
OnPropertyChanged("CountText");
}
}
wpf文本框:
// wpf textbox
<TextBox
Margin="10,0,0,0"
x:Name="CountText"
Style="{StaticResource EtchedTextBoxWhiteOnBlack}"
Background="Transparent"
Height="50" Width="200"
FontSize="22" FontWeight="ExtraBold"
Text="{Binding EventLogModel.CountText}"
TextAlignment="Center"
HorizontalAlignment="Center" VerticalAlignment="Center"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
更新模型中CountText属性的代码:
// model property to which the textbox is bound
public string CountText
{
get
{
return countText;
}
set
{
countText = value;
OnPropertyChanged("CountText");
}
}
// code snippet that is updating the datagrid and the string property to which the textbox is bound
bool bContinue = true;
int startIndex = 0;
int chunkSize = 50;
bool breset = EventLogSignalingEvent.Reset();
ThreadPool.QueueUserWorkItem(GetNumberOfLogEntries, lp);
bool bwait = EventLogSignalingEvent.WaitOne();
count = 0;
if (maxItems > 0)
{
UpdateStatusDialog("Loading log data", string.Format("{0} items total found", maxItems));
CountText = string.Format("{0} of {1}", count.ToString(), maxItems);
}
else
{
statusOpen = false;
UpdateStatusDialog("No log data found", "");
StatusDlgHideEllipse();
CountText = string.Format("{0} items found", maxItems);
}
while (bContinue && !bStopped)
{
EventLogSignalingEvent.Reset();
lp = new LogParams();
lp.chunkSize = chunkSize;
lp.eDate = eDate;
lp.sDate = sDate;
lp.startIndex = startIndex;
lp.UTCOffset = UTCOffset;
ThreadPool.QueueUserWorkItem(GetEventLogChunk, lp);
EventLogSignalingEvent.WaitOne();
if (chunk != null && chunk.Count > 0)
{
foreach (LogRecordDbMVVM db in chunk)
{
MainWindow.MainWinDispatcher.Invoke(DispatcherPriority.Send, new ThreadStart(() =>
{
try
{
EventLog.Add(LogRecordGUI.LogRecord_DBtoGUI(db));
Thread.Sleep(10);
}
catch
{
}
}));
if (statusOpen == true)
{
CloseStatusDialog();
statusOpen = false;
}
if (count < maxItems)
{
count++;
ThreadPool.QueueUserWorkItem(UpdateCount, count);
//CountText = string.Format("{0} of {1}", count.ToString(), maxItems);
}
}
}
else if (chunk == null)
{
log.Info("############################################### ERROR: chunk == null");
log.InfoFormat("Start date = {0} End date = {1} Start index = {2} chunkSize = {3} maxItems = {4}", sDate, eDate, startIndex, chunkSize, maxItems);
}
startIndex += chunkSize;
bContinue = (startIndex >= maxItems) ? false : true;
}
EventLogViewCount = (EventLog.Count - NumberOfEventLogItemsInView) >= 0 ? EventLog.Count - NumberOfEventLogItemsInView : 0;
}
catch (Exception ex)
{
log.ErrorFormat("{0} {1}", ex.Message, ex.StackTrace);
}
}
catch (Exception ex)
{
log.ErrorFormat("{0} {1}", ex.Message, ex.StackTrace);
}
}
private void UpdateCount(object context)
{
int count = (int)context;
CountText = string.Format("{0} of {1}", count.ToString(), maxItems);
}
private void GetNumberOfLogEntries(object context)
{
LogParams lp = (LogParams)context;
maxItems = BT220.ManagementService.GetEventLogCountChunk(lp.sDate, lp.eDate, lp.UTCOffset, lp.startIndex, lp.chunkSize);
EventLogSignalingEvent.Set();
}
我在这里看不到任何问题。。。您在寻求什么帮助?基础值的更新速度可能太快,UI线程没有机会运行。我的问题是,为什么在将每条记录添加到datagrid时计数没有得到更新?计数大约每300个条目更新一次。