0

我在 Viewbox 内玩 Canvas,不幸的是,查看 Canvas 中元素的唯一方法是,我必须为其分配高度和宽度。但是,问题是我的 Height 和 Width 值来自我的 ViewModel 通过数据绑定。

我知道 Blend 通过d:设置为xmlns:d="http://schemas.microsoft.com/expression/blend/2008". 我想对 VS2008 使用类似的东西,以便在设计器中我可以指定这些值并查看我正在使用的内容。

另一种选择是暂时对值进行硬编码,然后根据需要来回切换,但如果可能的话,我宁愿拥有最佳解决方案。任何信息将不胜感激!

更新——很奇怪,另一个问题是 kaxaml 和 VS2008 呈现方式不同。如果我在 kaxaml 中使用以下 XAML:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="3" />
            <Setter Property="Width" Value="50" />
        </Style>
    </Page.Resources>

    <DockPanel>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Height="Auto" HorizontalAlignment="Right">
            <Button>Cancel</Button>
            <Button>OK</Button>
        </StackPanel>
        <Grid DockPanel.Dock="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Viewbox Grid.Row="0">
                <Canvas Width="600" Height="50">
                    <TextBox>test</TextBox>
                </Canvas>
            </Viewbox>
            <Viewbox Grid.Row="1">
                <!-- <Canvas Width="{Binding WorkspaceWidth}" Height="{Binding WorkspaceHeight}" > -->
                <Canvas Width="640" Height="480" >    
                    <TextBox>test</TextBox>
                    <TextBox Canvas.Left="150" Canvas.Top="50">test</TextBox>
                </Canvas>
            </Viewbox>
        </Grid>
    </DockPanel>
</Page>

它按预期呈现。但是如果我将 DockPanel 复制到我的 VS2008 项目中,我只会得到“Viewbox”元素,里面什么都没有。任何人都可以解释这一点吗?

4

1 回答 1

0

一种方法是让您的 ViewModel 构造函数设置您想要的值,然后在设计时将其绑定到它:

<Window.Resources>
  <ResourceDictionary>
    <app:MyViewModel x:Key="ViewModel" />
  </ResourceDictionary>
</Window.Resources>

然后绑定:

<Grid
  DataContext="{StaticResource ViewModel}">
</Grid>

这不是一个完全令人满意的解决方案,但在编写代码时很好。

于 2010-08-15T17:53:25.583 回答