0

我试图让我的画布大小成为我在App.xaml.cs文件中定义如下的静态属性类的特定宽度/高度:

public class CanvasAttr
{
    public static double Width 
    { 
        get 
        {
            return Window.Current.Bounds.Width / 4;    
        } 
    }
    public static double Height 
    { 
        get 
        {
            return Window.Current.Bounds.Height / 4;    
        } 
    }
}

这是我的MainPage.xaml

<Page
    x:Class="Bossanova.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Bossanova"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="765.015">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <Style x:Key="DrawSurface" TargetType="Canvas">
                <Setter Property="Width" Value="{Binding CanvasAttr.Width}" />
                <Setter Property="Height" Value="{Binding CanvasAttr.Height}" />
                <Setter Property="Background" Value="AliceBlue" />
            </Style>
        </Grid.Resources>
        <Canvas x:Name="Main" Style="{StaticResource DrawSurface}">

        </Canvas>
    </Grid>
</Page>

如您所见,在<Style>定义中,我<Setter>为画布的 Width 和 Height 属性添加了标签。问题是一个简单的{Binding CanvasAttr.Width}似乎不会影响任何事情。我需要做什么才能做到这一点?

4

2 回答 2

2

使用x:Static绑定到 XAML 中的静态属性 -

<Setter Property="Width" Value="{x:Static local:CanvasAttr.Width}" />
<Setter Property="Height" Value="{x:Static local:CanvasAttr.Height}" />
于 2013-11-10T19:48:11.017 回答
0

不需要那种绑定。只需创建一个适当的网格布局:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="3*"/>
    </Grid.RowDefinitions>
    <Canvas x:Name="Main" Background="AliceBlue">

    </Canvas>
</Grid>

但是请注意,Canvas 仅对其子元素进行绝对定位。因此,设置画布的宽度或高度不会对其子级产生太大影响。

于 2013-11-12T08:43:21.520 回答