2009-08-29 85 views
2

我试图做这样的事情......是否可以在WPF中为2x2网格创建样式?

<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网格创建样式/模板?

谢谢。

回答

0

我很确定现在答案是:“无法完成”。如果我错了,请纠正我。

0

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> 
+0

感谢您的答案,但它不起作用。我得到这个错误:“Type'RowDefinitionCollection'不能用作对象元素,因为它不是公有的,或者没有定义一个公共无参数构造函数或类型转换器。” – devuxer 2009-08-29 00:13:32

+0

好的......也许这是不可能的 – 2009-08-29 09:13:44

相关问题