WPF可见性属性设计器问题

时间:2011-04-22 11:08:06

标签: wpf visual-studio designer visibility

我最近在设计时遇到了Visibility属性问题。一切都在运行时正常工作,但Visibility属性在设计时没有效果。

我重新安装了Visual Studio和.net框架,但问题仍然存在。

示例代码:

<StackPanel>
        <TextBlock Text="X" Visibility="Collapsed" Background="Red" />
        <TextBlock Text="Y"></TextBlock>
</StackPanel>

在运行时X折叠,而在设计时显示它。

2 个答案:

答案 0 :(得分:3)

安装VS2010 SP1解决了这个问题。 SP可在以下位置找到:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5

答案 1 :(得分:1)

更改代码后尝试Ctrl + Shift + B ..这可能对您有帮助..

但我建议您安装Microsoft Expression Blend。并在那里制作所有Xaml标记。


<强>更新

或者你可以使用它:

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

并像这样使用..

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

然后使用Ctrl + Shift + B