Are Expression Blend design-time specific visuals

2019-07-03 23:20发布

问题:

I'm trying to design some UserControl classes in Blend 3. I want parts of them to be "collapsed" when created at runtime, but I want to be able to edit their component parts without fiddling with code every time I want to build.

It works with sample datasources, as the following example illustrates. But it doesn't appear to work with other properties... or am I doing something wrong?

With a sample data source SDS_AIVertexAction We can do this in Expression Blend:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
...>


<Grid x:Name="LayoutRoot" 
    d:DataContext="{Binding Source={StaticResource SDS_AIVertexAction}}" >
    ...
</Grid>

But it does not seem to be possible to do this:

 <Label Content="{Binding Name}" Visibility="Collapsed" d:Visibility="Visible" />

I realise I could change visibility "on loaded" but I'd really rather not type all that guff every time I make a control like this. Does someone know a secret that lets us do this?

回答1:

Well, here's a guess.

The d: namespace is for stuff that is respected at design time but ignored at runtime. So we want to set the visibility somehow within the d: namespace where it overrides visibility set for runtime.

Inline styles override styles set globally or via StaticResource, so I'd suggest doing this (from memory--don't just copy and paste it, understand the concept):

<UserControl.Resources>
  <Style x:Key="invisible" TargetType="Label">
    <Setter Property="Visibility" Value="Collapsed"/>
  </Style>
</UserControl.Resources>
<!-- ... -->
<Label Style="{StaticResource invisible}" d:Visibility="Visible" />