【问题标题】:Bind ItemsPanelTemplate to property of ItemsSource将 ItemsPanelTemplate 绑定到 ItemsSource 的属性
【发布时间】:2020-10-09 05:35:41
【问题描述】:

我正在尝试创建一个具有可变数量按钮的自定义 MessageBox,此数字等于 ItemsSource 的计数。按钮将放置在 UniformGrid 中。 ButtonView 的 ItemsSource 被分配给 ObservableCollection Buttons。代码无法识别 Buttons.Count。如何正确分配 UniformGrid Columns 值?

    <ItemsControl Grid.Row="2" x:Name="ButtonView">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="{Binding Buttons.Count}">
                </UniformGrid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="Button">
                <Button Margin="2" Content="{Binding Content}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

【问题讨论】:

  • 请注意,当您将 UIElement 集合或派生类型分配给 ItemsControl 的 ItemsSource 时,ItemTemplate 将被忽略,因为这些项目不需要“模板化”。使用 Content="{Binding Content}" 创建新 Button 的 Button 的 DataTemplate 显然没有多大意义。

标签: wpf xaml itemscontrol


【解决方案1】:

设置Rows 属性而不是Columns

<ItemsPanelTemplate>
    <UniformGrid Rows="1"/>
</ItemsPanelTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2011-11-10
    • 2018-08-27
    • 2012-01-08
    • 2011-05-22
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多