Skip to main content
 首页 » 编程设计

c#之无法在我的 WPF 网格中创建列

2024年02月24日14lexus

我的非常非常基本的 WPF 项目有这段代码。

<Window x:Class="WpfApplication1.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300">    
 
<Grid ShowGridLines="True"> 
    <ColumnDefinition x:Name="LeftColumn"></ColumnDefinition> 
</Grid> 

但是,列定义行给我一个错误:

Error 1 Cannot add instance of type 'ColumnDefinition' to a collection of type 'UIElementCollection'. Only items of type 'UIElement' are allowed.

请您参考如下方法:

您必须将其包含在 ColumnDefinitions 集合中。

<Grid Height="27"> 
    <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
</Grid> 

添加行定义的方式相同。

尽情享受吧!