动态添加按钮到 ItemsControl
我有一个问题需要你的意见。我有一个外部网格,它有两列。在第一列中,我使用绑定到视图模型的 ItemsControl(此视图模型用于动态创建按钮)。我有一个为项目模板定义的包装面板模板。第二列用于列表框。
我在视图模型中为按钮创建对象,并将其绑定到视图。要求是,如果窗口高度适合,则所有按钮应出现在单个列中(一个在另一个列下面),否则应出现在两列中。
如果一栏
1
2
3
4
如果有两列:
1 2
3 4
下面给出了我的代码:
ItemsControl:
<Grid>...
<ItemsControl ItemsSource="{Binding buttonsViewModelColl}"
ItemsPanel="{StaticResource WrapPanelTemplateA}"
ItemTemplate="{StaticResource ButtonsDataTempleteA}" Width="{Binding ActualWidth, ElementName=<OuterGrid>}" Grid.Row="2" HorizontalAlignment="Center"/>
</Grid>
WrapPannelTemplete:
<ItemsPanelTemplate x:Key="WrapPanelTemplate">
<WrapPanel/>
</ItemsPanelTemplate>
DataTempleteForButton :
<DataTemplate x:Key="ButtonsDataTempleteA">
<Button
Content="{Binding Path=Text}"
IsEnabled="{Binding Path=IsEnabled}"
Style="{StaticResource StyleButtonA}"
FontFamily="Segoe UI Semibold"
HorizontalAlignment="Left" />
</DataTemplate>
问题是在渲染时只有一两个按钮可见。如果我将 WrapPannel Orientation 设置为垂直,所有内容都会出现,但不会自动换行。仅当我拖动并扩展窗口的宽度时,所有内容都是可见的。而且序列不正确:
它们是:
A C
BD
任何想法。
另一个问题:有没有办法可以在 XAML 运行时根据外部网格高度增加内部网格列的宽度?
I have a issue that i want your inouts on. I have a outer grid and it has two columns. On first column i am using a ItemsControl that is bind to a a view model (This view model is for dynamically creating buttons). I have a wrap pannel templete define for the Item templete. The 2nd column is for a list box.
I create the objects for the buttons in the view model and that gets binded to the view.The requiremnet is that if the window hieight fits then all buttons should appear in a single colum (one below the other) else it should come in two columns.
If one column
1
2
3
4
If Two Columns :
1 2
3 4
The code i have is given below :
ItemsControl:
<Grid>...
<ItemsControl ItemsSource="{Binding buttonsViewModelColl}"
ItemsPanel="{StaticResource WrapPanelTemplateA}"
ItemTemplate="{StaticResource ButtonsDataTempleteA}" Width="{Binding ActualWidth, ElementName=<OuterGrid>}" Grid.Row="2" HorizontalAlignment="Center"/>
</Grid>
WrapPannelTemplete:
<ItemsPanelTemplate x:Key="WrapPanelTemplate">
<WrapPanel/>
</ItemsPanelTemplate>
DataTempleteForButton :
<DataTemplate x:Key="ButtonsDataTempleteA">
<Button
Content="{Binding Path=Text}"
IsEnabled="{Binding Path=IsEnabled}"
Style="{StaticResource StyleButtonA}"
FontFamily="Segoe UI Semibold"
HorizontalAlignment="Left" />
</DataTemplate>
The problem is that on rendering only one or two buttons are visible. If i set teh WrapPannel Orientation to vertical all comes but they donot wrap automatically. Only if i drap and extend teh width of window all are visible. Also teh sequence is not correct :
They come as :
A C
B D
Any Ideas.
Another question : Is there a way i can increase the width of the inner grid column based on an outer grid height at runtime in XAML ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要的是
UniformGrid
而不是WrapPanel
因为您希望按钮总数、整个容器的高度和面板中的列数相互依赖。包裹面板无法控制第三个因素(即面板中的列数)。UniformGrid
具有完全可绑定的Rows
和Columns
属性。它们可以与上述 3 个因素进行多重绑定,并且多值转换器可以根据某些自定义启发式算法决定要显示的行数和列数。我希望这能引导您走向正确的方向。
I think what you need is a
UniformGrid
notWrapPanel
because you want the total number of buttons, the height of the entire container and the number of columns in the panel to be interdependent. Wrap Panel cannot control the third factor (i.e. number of columns in the panel).UniformGrid
hasRows
andColumns
property which are perfectly bindable. They can be multi bound to the above 3 factors and a multi value converter can decide what number of rows and columns to display based on some custom heuristic algorithm.I hope this guides you in correct direction.
我怀疑这与 ItemsControl 允许的宽度有关。如果手动设置 WrapPanel 的宽度是否有效?
至于你的最后一个问题,根据网格的高度设置列的宽度,你可以简单地绑定到它。
如果您需要对该值执行计算,我通常使用转换器,它接受网格高度的参数,并将其转换为我想要的任何值。我实际上有一个用于此类事情的
MathConverter
,因为我懒得为我想要的每个绑定计算编写自己的转换器,并且可以找到代码 这里它可以这样使用:
实际上,你可以使用
MathConver
设置WrapPanel
的Width
,它应该可以正常工作。I suspect it has something to do with the Width the ItemsControl is allowed. Does it work if you set the WrapPanel's Width manually?
As for your last question, setting the column's width based on the Grid's height, you can simply bind to it.
If you need to perform a calculation on that value, I usually use a Converter which accepts a parameter of the Grid's height, and translates it into whatever value I want. I actually have a
MathConverter
that use for this kind of thing because I'm too lazy to write my own converter for every binding calculation I want, and the code can be found hereIt can be used like:
Actually, you can probably use the
MathConver
to set theWidth
of yourWrapPanel
, and it should work correctly.