I've recently started having issues with Visibility attribute at designtime. Everything works fine at runtime but the Visibility attribute has no effect at designtime.
I've reinstalled Visual S开发者_运维百科tudio and the .net framework but the issue persists.
Sample code:
<StackPanel>
<TextBlock Text="X" Visibility="Collapsed" Background="Red" />
<TextBlock Text="Y"></TextBlock>
</StackPanel>
At runtime X is collapsed while at designtime it is shown.
Installing VS2010 SP1 fixed the issue. SP can be found at: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5
try Ctrl + Shift + B
after you changed code.. this maybe help you..
But i recomend you to install Microsoft Expression Blend. And make all Xaml markup, up there.
Update:
Or you can use this:
public class VisibilityFixer: DependencyObject
{
public static bool GetFixDesigner(DependencyObject obj)
{
return (bool)obj.GetValue(FixDesignerProperty);
}
public static void SetFixDesigner(DependencyObject obj, bool value)
{
obj.SetValue(FixDesignerProperty, value);
}
// Using a DependencyProperty as the backing store for FixDesigner. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FixDesignerProperty =
DependencyProperty.RegisterAttached("FixDesigner", typeof(bool), typeof(VisibilityFixer),
new UIPropertyMetadata(false, new PropertyChangedCallback(PropertyChanged)));
public static void PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var obj = sender as FrameworkElement;
if (obj != null)
{
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
if (obj.Visibility == Visibility.Collapsed)
{
obj.Opacity = 0;
obj.Height = 0;
obj.Width = 0;
}
else if (obj.Visibility == Visibility.Hidden)
{
obj.Opacity = 0;
}
}
}
}
}
and use it like this..
<StackPanel x:Name="LayoutRoot">
<TextBlock TextWrapping="Wrap"
Visibility="Collapsed"
fx:VisibilityFixer.FixDesigner="True"
Text="TextBlock3243" HorizontalAlignment="Left"
Background="Red" />
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap"><Run Text="TextBlock"/></TextBlock>
</StackPanel>
and then use Ctrl + Shift + B
精彩评论