Using system types in XAML as resources

爱⌒轻易说出口 提交于 2019-12-04 22:37:19

No. ColumnDefinition.Width is of Type GridLength which is why you're getting the error. If you do something like the code below, it should work fine.

<Window.Resources>
    <core:Double x:Key="MyDouble">300</core:Double>
    <GridLength x:Key="MyGridLength">20</GridLength>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource MyGridLength}" />
        <ColumnDefinition Width="40" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Rectangle Grid.Column="0" Fill="Red" />
    <Rectangle Grid.Column="1" Fill="Green" />
    <Rectangle Grid.Column="2" Fill="Blue"  Width="{StaticResource MyDouble}"/>

</Grid>

The problem you are encountering is that on the ColumnDefinition object, the Width property is NOT a double, it is a GridLength structure. If you look at the MSDN documentation for ColumnDefinition.Width you'll see that you cannot assign a double to a ColumnDefinition.Width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!