如何将滚动条添加到WPF中的wrappanel控件.由于没有滚动条,我的一些控件正在切断宽度.
我有一个显示项目的包装面板,但我不能让滚动条正常工作任何想法的错误是什么?
<ScrollViewer>
<ItemsControl Name="itemsControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel ItemWidth="{Binding ElementName=sizeSlider, Path=Value}"
FlowDirection="LeftToRight" Height="auto" Width="auto"
HorizontalAlignment="Left" Name="wrapPanel1"
VerticalAlignment="Top"
Margin="5"
>
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
WrapPanel的子项按顺序填充,如附加的屏幕截图.
因此,根据每个孩子的长度,面板会留出很长的空白.
如何利用空白区重新安排孩子们?
到目前为止,似乎只有少数人使用WrapPanel而且没有足够的例子.
这有什么自动方式吗?或者我只需要制作自己的算法?
WrapPanel在显示事物方面起着非常重要的作用,但显示空间有限.
谢谢 !
好的,这是我的代码.请注意,StackPanel直接位于UserControl中.
<UserControl>
<StackPanel Orientation="Horizontal">
<ScrollViewer Width="450"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<Rectangle Width="400" Height="4000" Fill="BlanchedAlmond"/>
</ScrollViewer>
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ItemsControl BorderBrush="Green"
BorderThickness="2"
ItemsSource="{Binding Path=MyObservableCollection}"
ItemTemplate="{StaticResource FatTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
(代码已编辑)
请注意,矩形就在那里,因为该滚动查看器的内容无关紧要.
问题是WrapPanel只是水平扩展而不包裹......
我找到了一些解决方案:
为WrapPanel提供绝对宽度(但随后窗口不会调整大小).
将宽度绑定到其父宽度 - ItemsControl或ScrollViewer(ScrollViewer在像这样的有限情况下工作得更好).
Width="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type ScrollViewer}},
Path=ActualWidth}"
Run Code Online (Sandbox Code Playgroud)这种技术的问题在于,如果一个控件在StackPanel或Grid旁边,你需要将它绑定到它的父级大小减去旁边的控件.然后是艰难的东西.我构建了一个转换器,它对接收的数字应用数学运算,因此我可以从父宽度中减去给定的宽度:
Width="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type UserControl}},
Path=ActualWidth,
Converter={StaticResource Convertisseur_Size_WXYZ},
ConverterParameter=-260}"
Run Code Online (Sandbox Code Playgroud)
但是,由于您无法绑定传递给ConverterParameter的值,
ConverterParameter={Binding ...} (doesn't work)
Run Code Online (Sandbox Code Playgroud)
我想要:
什么是更好的解决方案?
注意:如果有任何不清楚的地方,我可以添加细节.
我有MouseDown事件的问题.我的应用程序看起来像这样,我有网格,其中我在代码后面添加按钮
<Grid Grid.Column="1" Name="gridWithButtons" MouseDown="normalModeButtonsWP_MouseDown" >
<WrapPanel Name="normalModeButtonsWP" MouseDown="normalModeButtonsWP_MouseDown" ></WrapPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但是当我在网格/ wrappanel中按下鼠标按钮时(我的意思是在按钮之间的空白区域)它不起作用.仅当我按下包裹/网格中的按钮时才有效.有谁知道如何处理它?
我被迫寻求帮助,而我自己却无法理解.我正在研究WPF-XAML桌面应用程序,其中GUI主要是动态生成的.
我的查询关于WrapPanel与ListBox项的样式.
请从我的usercontrol(.xaml)中找到一段代码:
<DockPanel x:Name="xResultPanel">
<ListView x:Name="bResultPanel" ItemsSource="{Binding ResultList, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding GroupName}" Style="{DynamicResource FeatureExpander2}">
<WrapPanel ItemWidth="140" Orientation="Horizontal">
<ListBox x:Name="ListOfTiles" ItemsSource="{Binding VideoSamples}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="120" Margin="10" HorizontalAlignment="Left">
<Image />
<TextBlock />
</StackPanel
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WrapPanel>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
上面的代码返回不是连续显示的ListBox项目,而是返回新行中的每个项目. 我试图为WrapPanel和ListBox设置MinWidth,Width等,但没有结果.
提前感谢所有相关技巧如何强制WrapPanel水平填充内容.
我有一个WrapPanel和按钮以编程方式创建并添加为WrapPanel的子项.因此,当WrapPanel充满按钮(子)时,我想显示垂直滚动条,以便能够连续添加更多按钮.
如果我们需要显示滚动条,我们是否必须携带ScrollViewer?没有ScrollViewer没有办法吗?我想得到的是,因为WrapPanel的尺寸很小,我希望只在需要时显示一个滚动条(就像孩子一样).
我的代码很简单如下(Grid中的WrapPanel和GridControl内的Grid)
非常感谢您的卓越.
更新:我努力在互联网上寻找解决方案甚至好几天.我尝试将WrapPanel放在ScrollViewer中.但是,虽然我将VerticalScroll设置为auto,但即使WrapPanel没有任何子项,也会始终显示垂直滚动条.
此外,当我故意让WrapPanel充满子项(按钮)时,ScrollViewer的垂直滚动条不提供向下滚动可用性.并且WrapPanel底线的按钮显示剪切更多,我无法向下滚动以查看底部按钮以外的按钮.我故意将按钮放在WrapPanel的底线之外.
有或没有,我希望只在需要时显示垂直滚动条(充满孩子).这似乎很容易完成.但它很难使其正常工作.
解决方案:由Henk Holterman先生提供
<DropShadowEffect/>
</Button.Effect>
</Button>
<WrapPanel x:Name="WrapPanelGreen" HorizontalAlignment="Left" Height="180" VerticalAlignment="Top" Width="232" UseLayoutRounding="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</Grid>
</TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
下面是我的简单代码,它以编程方式创建按钮并添加为WrapPanel的子代.
for (int k = 0; k < Overviews.Length; k++)
{
Button btnoverviewcontent = new Button();
ToolTip tt = new ToolTip();
tt.Content = "Press this button if you want to modify or delete.";
btnoverviewcontent.ToolTip = tt;
btnoverviewcontent.Cursor = Cursors.Hand;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241);
btnoverviewcontent.Background = mySolidColorBrush;
btnoverviewcontent.Effect …Run Code Online (Sandbox Code Playgroud) 我在WPF中使用WrapPanel来显示图像.调整表单大小时,这些图像会尝试占用最大的自由空间,但是当图片几乎可以填充空间时,WrapPanel的末尾会有一个相对较大的间隙.我想要做的是在新的填充这个空白之前在图像之间共享这个空间(就像Windows 7 Explorer在显示图像图标时所做的那样).
我想要 1.

我的代码在这里
<WrapPanel>
<TextBlock Text="Title: " Style="{StaticResource Title}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Description" Style="{StaticResource Normal}" TextWrapping="Wrap" />
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)
但.如果Description文本很短,如图所示2.,如果Description文本很长,则显示为3.
怎么样1.?
在过去的几天里,我一直在处理backgroundWorker的问题.我一直在浏览MSDN上的论坛和文档,但仍然没有找到答案,所以现在我想问你聪明的人.
长话短说,我有一个自定义用户控件,由ScrollViewer中的WrapPanel组成.WrapPanel包含一些在滚动到视图时通知的元素.然后,这些元素将被加载并显示图像,这就是问题所在的地方.为了不锁定gui线程,我将图像加载到BackgroundWorker中,但GUI仍然会停止.这是表示WrapPanel中包含的元素的类的代码:
class PictureThumbnail : INotifyingWrapPanelElement
{
private string path;
private Grid grid = null;
private BackgroundWorker thumbnailBackgroundCreator = new BackgroundWorker();
private delegate void GUIDelegate();
private Image thumbnailImage = null;
public PictureThumbnail(String path)
{
this.path = path;
visible = false;
thumbnailBackgroundCreator.DoWork += new DoWorkEventHandler(thumbnailBackgroundCreator_DoWork);
}
void thumbnailBackgroundCreator_DoWork(object sender, DoWorkEventArgs e)
{
BitmapImage bi = LoadThumbnail();
bi.Freeze(); //If i dont freeze bi then i wont be able to access
GUIDelegate UpdateProgressBar = delegate
{
//If this line is commented out the …Run Code Online (Sandbox Code Playgroud)