【问题标题】:How to view collapsed elements while designing a WPF control?设计 WPF 控件时如何查看折叠的元素?
【发布时间】:2012-03-26 08:31:44
【问题描述】:

我将我的 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() };
        }
    }

但是没用

【问题讨论】:

  • BooleanToVisibilityConverter 在做什么?
  • 我认为它会将 true 转换为“可见”,将 false 转换为“折叠”。

标签: c# .net wpf datagrid visibility


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2015-02-09
      • 2012-01-26
      • 2022-01-03
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2012-12-07
      相关资源
      最近更新 更多