【问题标题】:Is it possible to create a Style for a 2x2 Grid in WPF?是否可以在 WPF 中为 2x2 网格创建样式?
【发布时间】:2009-08-29 00:02:32
【问题描述】:

我正在尝试做这样的事情......

<Style
    x:Key="TwoByTwoGridStyle"
    TargetType="Grid">
    <Setter
        Property="Grid.RowDefinitions">
        <Setter.Value>
            <ControlTemplate>
                <RowDefinition
                    Height="*" />
                <RowDefinition
                    Height="Auto" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter
        Property="Grid.ColumnDefinitions">
        <Setter.Value>
            <ControlTemplate>
                <ColumnDefinition
                    Width="*" />
                <ColumnDefinition
                    Width="Auto" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ControlTemplate 不对。我收到错误消息:“属性VisualTree 不支持RowDefinition 类型的值”。有什么方法可以表示行/列定义的集合吗?或者,是否有其他方法可以为 2x2 网格创建样式/模板?

谢谢。

【问题讨论】:

    标签: wpf xaml styles


    【解决方案1】:

    RowDefinitions 属性不是 ControlTemplate 类型,因此为其分配 ControlTemplate 没有意义。您应该分配一个 RowDefinitionCollection :

    <Style
        x:Key="TwoByTwoGridStyle"
        TargetType="Grid">
        <Setter
            Property="Grid.RowDefinitions"
            <Setter.Value>
                <RowDefinitionCollection>
                    <RowDefinition
                        Height="*" />
                    <RowDefinition
                        Height="Auto" />
                </RowDefinitionCollection>
            </Setter.Value>
        </Setter>
        <Setter
            Property="Grid.ColumnDefinitions"
            <Setter.Value>
                <ColumnDefinitionCollection>
                    <ColumnDefinition
                        Width="*" />
                    <ColumnDefinition
                        Width="Auto" />
                </ColumnDefinitionCollection>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 感谢您的回答,但它不起作用。我收到此错误:“类型 'RowDefinitionCollection' 不能用作对象元素,因为它不是公共的或未定义公共无参数构造函数或类型转换器。”
    • 好吧...也许那是不可能的
    【解决方案2】:

    我现在很确定答案是:“做不到”。如果我错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2014-01-14
      相关资源
      最近更新 更多