Wpf 将ItemsControl绑定到ScrollViewer
首先,我将展示一些代码:Wpf 将ItemsControl绑定到ScrollViewer,wpf,itemscontrol,scrollviewer,itemssource,Wpf,Itemscontrol,Scrollviewer,Itemssource,首先,我将展示一些代码: private ObservableCollection<otwarteBezStolika> otwarteBezStolika = new ObservableCollection<otwarteBezStolika>(); public ObservableCollection<otwarteBezStolika> listunia { get { return otwa
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; }
}
private observateCollection otwarteBezStolika=new observateCollection();
公共可观测收集列表
{
获取{return otwarteBezStolika;}
设置{otwarteBezStolika=value;}
}
}
公共类Otwartebeztolika
{
公共字符串编号{get;set;}
公共字符串日期{get;set;}
公共int-orderID{get;set;}
公共十进制价格{get;set;}
公共十进制价格{get;set;}
公共字符串opisRach{get;set;}
公共字符串和{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>
网格:
问题是我看不到任何项目(我正在使用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>
但项目是垂直显示的,但我需要看到它们从左到右水平
谢谢你的回答 试着这样做。。您不需要main模板
<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>
这将以单间距字体显示。前四个空格
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="kaloo" DisplayMemberPath="Name" ss:DragDrop.IsDragSource="True" ss:DragDrop.IsDropTarget="True" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ScrollViewer>
好的,它现在确实显示了项目-但屏幕中间只有一个:)我已经将StackPanel的宽度和高度更改为150/70,我确信它应该显示更多,因为“listunia”有71个项目它是有效的吗?你们有多少件?如何设置视图的datacontext?最后一个元素“UI”是什么?对不起,76不是71。我可以使用垂直/水平对齐来显示它们,但看起来scrollviewer的宽度设置不正确。现在显示的项目类似于:[]],而不是类似于[]]“输入”[]]“输入”[]]“输入”[]]“输入”[]]:)为什么不使用没有任何项目的简单列表框呢?也不要使用scrollviewer…itemscontrol将显示其自动滚动。它不能是listview,因为我对每个项目都有适当的样式。它必须是“按钮”,里面有stackPanel(显示信息等),所以点击后会转到另一个窗口。是的,我知道我可以在listview中使用selectionchanged,但我想把它保留为按钮。但仍然显示出错误,正如我之前在帖子中所说:)
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="kaloo" DisplayMemberPath="Name" ss:DragDrop.IsDragSource="True" ss:DragDrop.IsDropTarget="True" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ScrollViewer>