开发者

Specifying a WPF's databound attribute's design-time value in XAML

I'm playing around with a Canvas inside of a Viewbox, and unforunately, the only way to see the elements in the Canvas, I have to assign it a Height and Width. However, the problem is that my Height and Width values come from my ViewModel via databinding.

I know that Blend gets around this with the d: designer namespace, which is set to xmlns:d="http://schemas.microsoft.com/expression/blend/2008". I'd like to use something similar for VS2008, so that in the designer I can specify those values and see what I'm wo开发者_高级运维rking with.

The other option is to hardcode the values for now and then switch back and forth as necessary, but I'd rather have the optimal solution, if possible. Any info would be much appreciated!

UPDATE -- weird, another problem is that kaxaml and VS2008 render differently. If I use the following XAML in kaxaml:

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

it renders as expected. But if I copy the DockPanel into my VS2008 project, I just get "Viewbox" elements with nothing inside. Can anyone explain this as well?


One way is to have your ViewModel constructor set up the values you want, and then bind it to it at design time:

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

then bind:

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

It's not a completely satisfactory solution, but is good when writing the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜