2014-11-19 2 views
0

У меня есть элемент управления.Почему 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); 
}); 

EDIT:

Для добавления вызовов, я использовал 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

Вместо 'Dispatcher.BeginInvoke' попробуйте использовать' Dispatcher.Invoke' для добавления элементов в коллекцию. –

+0

@RohitVats, тот же результат я получил. –

+0

Я не вижу никакого цикла для добавления элементов в коллекцию. Откуда вы вызываете этот метод? –

ответ

0

И, наконец, элемент ItemsControl не так хорошо работает с ScrollViewer, поэтому я не вижу все элементы. Удаляя элемент ScrollViewer и добавьте в шаблон управления в 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> 
+0

FWIW, я бы также обернул ваш ItemsControl в Grid. – Rod

Смежные вопросы