2014-11-19 3 views
0

ItemsControl이 있습니다. 내 MainViewModel.cs에서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> 

, 내가 가진 :

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); 
}); 

는 편집 :

전화를 추가하려면, 나는 비동기 Task 방법에 ActionBlock을 사용했다.

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 세트의 데이터가 있어야합니다. 나는 그들이 null이 아니라고 확신합니다. 그러나 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

관련 문제