将ItemsControl绑定到ScrollViewer

时间:2013-09-17 12:09:30

标签: wpf itemscontrol scrollviewer itemssource

在开始时我会展示一些代码:

private ObservableCollection<otwarteBezStolika> otwarteBezStolika = new ObservableCollection<otwarteBezStolika>();

        public ObservableCollection<otwarteBezStolika> listunia
        {
            get { return otwarteBezStolika; }
            set { otwarteBezStolika = value; }
        }
    }
    public class otwarteBezStolika
    {
        public string number { get; set; }
        public string date { get; set; }
        public int orderID { get; set; }
        public decimal price { get; set; }
        public decimal priceR { get; set; }
        public string opisRach { get; set; }
        public string sum { get; set; }
    }

现在在xaml:

<Window.Resources>
    <DataTemplate x:Key="dataTempl">
        <Border BorderBrush="Coral" BorderThickness="1" Width="170">
            <Button Name="goToPOS" Tag="{Binding orderID}" Click="goToPOS_Click" Style="{StaticResource TabCloseButtonStyle}" Margin="1">
                <StackPanel>
                    <Label Content="{Binding number}" HorizontalAlignment="Center" FontSize="15" FontWeight="ExtraBold" HorizontalContentAlignment="Center"></Label>
                    <Border BorderBrush="Turquoise" BorderThickness="1" Width="170"></Border>
                    <Label Content="{Binding date}" FontSize="12" HorizontalAlignment="Center"></Label>
                    <TextBlock Text="{Binding opisRach}" FontStyle="Italic" FontSize="12" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Margin="0,0,0,2"></TextBlock>
                    <Border BorderBrush="WhiteSmoke" BorderThickness="1" Width="170"></Border>
                    <Label Content="{Binding sum}" FontSize="19" HorizontalAlignment="Center"></Label>
                </StackPanel>
            </Button>
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="mainTemplate">
        <StackPanel>

            <ItemsControl x:Name="imageContent" ItemsSource="{Binding listunia}" ItemTemplate="{StaticResource dataTempl}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

网格:

<Grid Grid.Row="0">
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource mainTemplate}" />
        </ScrollViewer>
    </Grid>

问题是我看不到任何项目(我正在使用sqldatareader填充项目,并将它们添加到列表中 - 顺便说一下,DataTable也可以工作吗?所以相反,当(rdr.Read())我可以SqlDataAdapter sda和sda.fill(Datatable))

第二个问题是,当我将“dataTempl”放在scrollviewer中时,它确实有效,如:

<Grid Grid.Row="0">
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}" />
        </ScrollViewer>
    </Grid>

但是项目是垂直显示的,但我需要从左到右看水平。

感谢您的回答!

2 个答案:

答案 0 :(得分:2)

尝试这样做..你不需要maintemplate。

<ScrollViewer VerticalScrollBarVisibility="Auto">
  <ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}">
    <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
               <StackPanel Orientation="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
           </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
  </ItemsControl>
</ScrollViewer>

或者你可以简单地使用:

  <ListBox Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}">
  </ListBox>

答案 1 :(得分:0)

这将以等宽字体显示。前四个空格

<ScrollViewer VerticalScrollBarVisibility="Auto">
        <ItemsControl Name="kaloo" DisplayMemberPath="Name" ss:DragDrop.IsDragSource="True" ss:DragDrop.IsDropTarget="True"  >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
 </ScrollViewer>