如何在设计WPF控件时查看折叠元素?

时间:2012-03-08 20:53:03

标签: c# .net wpf datagrid visibility

当没有项目

时,我将我的DataGrid设置为折叠
<DataGrid Name="dataGrid"
          Visibility="{Binding HasItems,
                       ElementName=dataGrid,
                       Converter={StaticResource BooleanToVisibilityConverter}}">
</DataGrid>

问题是,我希望它出现在设计模式中。怎么做?我应该创建虚假数据吗?

我试过

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            this.dataGrid.ItemsSource = new List<Table> { new Table() };
        }
    }

但它无效

2 个答案:

答案 0 :(得分:3)

如果我理解你想要做什么,这对我很有用:

<Window x:Class="WPFScratch.MainWindow"
    xmlns:local="clr-namespace:WPFScratch"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    d:DesignHeight="300"  
    d:DesignWidth="592"  
    d:DataContext="{d:DesignInstance local:MyDesignTimeViewModel, IsDesignTimeCreatable=True}"
    Title="MainWindow" SizeToContent="WidthAndHeight">
<Window.Resources>
    <local:BoolToVisibilityConverter
     x:Key="BoolToHiddenConverter"
     TrueValue="Visible" FalseValue="Hidden" />        
</Window.Resources>
<DockPanel>       
    <DataGrid Name="dataGrid" ItemsSource="{Binding People}"
      Visibility="{Binding HasItems,
                   ElementName=dataGrid,
                   Converter={StaticResource BoolToHiddenConverter}}" AutoGenerateColumns="True">

    </DataGrid>
</DockPanel>

public class MyDesignTimeViewModel
{
    public ObservableCollection<Person> People
    {
        get 
        { 
            return new ObservableCollection<Person> { 
                                                        new Person 
                                                            { 
                                                                Name = "Simon" 
                                                            },
                                                        new Person 
                                                            { 
                                                                Name = "Jack" 
                                                            } 
                                                    }; 
        }
    }
}

public class Person
{
    public string Name { get; set; }
}

答案 1 :(得分:1)

我在Silverlight应用程序中遇到了类似的问题,我不希望在用户登录之前向用户显示任何内容。我在View的构造函数中将View的可见性设置为“Collapsed”,然后在用户进行身份验证/授权后返回“可见”。我建议您在View的构造函数中绑定Grid的Visibility属性,这样在代码执行之前这不会生效,从而让您在设计视图中看到Grid。我在WPF中做的不多,但是这样的事情可能有用:

Binding b = new Binding("Visibility");
b.Source = dataGrid.HasItems;
b.Converter = new BooleanToVisibilityConverter();
BindingOperations.SetBinding(dataGrid, VisibilityProperty, b);

同样,我不知道这是否适用于WPF,但也许这会让你更接近。