在 XAML 中指定 WPF 的数据绑定属性的设计时值
我正在 Viewbox 内使用 Canvas,不幸的是,查看 Canvas 中元素的唯一方法是,我必须为其分配高度和宽度。然而,问题是我的高度和宽度值来自我的 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”元素,里面什么也没有。任何人都可以解释一下吗?
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 working 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是让 ViewModel 构造函数设置所需的值,然后在设计时将其绑定到它:
然后绑定:
这不是一个完全令人满意的解决方案,但在编写代码时很好。
One way is to have your ViewModel constructor set up the values you want, and then bind it to it at design time:
then bind:
It's not a completely satisfactory solution, but is good when writing the code.