【问题标题】:Using a UniformGrid inside of a Button control in WPF在 WPF 中的 Button 控件内使用 UniformGrid
【发布时间】:2015-07-16 22:00:24
【问题描述】:

我希望 Button 控件的内容包含统一的网格。但是,Button 控件似乎不允许其子内容控件进行拉伸。我的 UniformGrid 总是被压缩以适应它自己的内容。它允许我设置宽度,但即使我明确设置了 Horizo​​ntalAlignment,它也不会自动拉伸。我错过了什么?

        <Button Margin="0,4,0,4">
            <ItemsControl ItemsSource="{Binding Zones}" Focusable="False">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="{Binding Zones.Count}" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <wpfApplication2:ControllerZoneControl DataContext="{Binding}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Button>

我可以使用以下命令强制设置宽度,但对我来说这似乎很老套。

 <UniformGrid Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=ActualWidth}" Columns="{Binding Zones.Count}" />

【问题讨论】:

  • 你能把你的“ControllerZoneControl”中的代码贴出来吗?
  • 您的“hack”通常是正确的解决方案。但是你不能将Button的HorizontalContentAlignment设置为Stretch吗?
  • 这是拼图中缺失的部分!

标签: wpf wpf-controls


【解决方案1】:

在我忘记尝试的所有事情中...将按钮的 Horizo​​ntalContentAlignment 设置为 Stretch 是解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2017-11-17
    相关资源
    最近更新 更多