我有一个ItemsControl。
<DockPanel Height="280">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" >
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Height="400" Name="icTodoList" ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Name="ToDoList">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding StartTime, FallbackValue=' '}" Grid.Row="0"/>
<TextBlock Text="{Binding ConnectedTime, FallbackValue=' '}" Grid.Row="1"/>
<TextBlock Text="{Binding DisconnectedTime, FallbackValue=' '}" Grid.Row="2"/>
<TextBlock Text="{Binding DialingResult, FallbackValue=' '}" Grid.Row="3"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Border>
</DockPanel>
在我的MainViewModel.cs中,我有:
public class MainViewModel : NotifyUIBase
public ObservableCollection<Calls> items = new ObservableCollection<Calls>();
public ObservableCollection<Calls> Items
{
get { return items; }
set
{
items = value;
RaisePropertyChanged();
}
}
和
public class NotifyUIBase : INotifyPropertyChanged
{
// Very minimal implementation of INotifyPropertyChanged matching msdn
// Note that this is dependent on .net 4.5+ because of CallerMemberName
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在我的代码背后: MainViewModel _dataContext;
public MainWindow()
{
InitializeComponent();
_dataContext = new MainViewModel();
this.DataContext = _dataContext;
Dictionary<string, string> dict = new Dictionary<string, string>();
dict = RunScript();
Calls c = new Calls();
c.StartTime = dict["StartTime"];
c.ConnectedTime = dict["ConnectedTime"];
c.DisconnectedTime = dict["DisconnectedTime"];
c.DialingResult = dict["DialingResult"] + '\n';
Dispatcher.BeginInvoke((Action)delegate()
{
if (c != null)
_dataContext.Items.Add(c);
});
编辑:
要添加通话,我在异步ActionBlock
方法中使用了Task
。
var actionBlock = new ActionBlock<T>(
t=>
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict = RunScript(t);
Calls c = new Calls();
// get c from dict
Dispatcher.BeginInvoke((Action)delegate()
{
if (c != null)
_dataContext.Items.Add(c);
});
},
executionDataflowBlockOptions);
// link a BufferBlock to this ActionBlock
await actionBlock.Completion;
假设我有100个调用,屏幕上的预期输出应该有100个数据集。我确信它们不是空的。但是它只显示6个数据集,为什么?
答案 0 :(得分:0)
最后,一个ItemsControl与ScrollViewer不能很好地工作,这就是我没有看到所有项目的原因。通过删除ScrollViewer元素并通过设置其Template属性将其添加到ItemsControl的控件模板:
<DockPanel Height="280">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" >
<ItemsControl Name="icTodoList" ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Name="ToDoList">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding StartTime, FallbackValue=' '}" Grid.Row="0"/>
<TextBlock Text="{Binding ConnectedTime, FallbackValue=' '}" Grid.Row="1"/>
<TextBlock Text="{Binding DisconnectedTime, FallbackValue=' '}" Grid.Row="2"/>
<TextBlock Text="{Binding DialingResult, FallbackValue=' '}" Grid.Row="3"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Border>
</DockPanel>