2011-04-02 76 views
2

我喜欢设计时数据,特别是在创建小部件时。对于这个非常简单的用例,我无法绑定到我在xaml中创建的设计时间列表的属性。C#/ WPF - DesignData - 绑定到DesignData集合属性

请在下面找到我的ViewModel,View和SampleData;

视图模型

internal class SummaryViewModel : ViewModelBase 
{ 
    public string Title { get; set; } 

    public IList<Person> PersonList { get; set; } 

    internal SummaryViewModel() 
    { 
     PersonList = new List<Person>(); 
    } 
} 

样本数据

<ViewModel:SummaryViewModel xmlns:ViewModel="ViewModel" Title="Test Title"> 
    <ViewModel:SummaryViewModel.Connections> 
     <ViewModel:ConnectionViewModel Id="0" /> 
     <ViewModel:ConnectionViewModel Id="1" /> 
    </ViewModel:SummaryViewModel.Connections> 
</ViewModel:SummaryViewModel> 

查看

<StackPanel x:Class="View.SummaryView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

      mc:Ignorable="d" 
      d:DesignHeight="100" 
      d:DesignWidth="100" 
      d:DataContext="{d:DesignData Source=/DesignData/SampleSummaryViewModel.xaml}" 

      Orientation="Vertical" 
      Background="LightGreen"> 

    <!-- This Works --> 
    <Label FontSize="10" FontWeight="Bold" Content="{Binding Title}" /> 

    <!-- This Works --> 
    <ListBox ItemsSource="{Binding PersonList}" /> 

    <!-- This DOESN'T work --> 
    <Label FontSize="8" Content="{Binding PersonList, Path=Count}"/> 
</StackPanel> 

如何配置SampleData以便可以绑定到其中指定的列表的计数?

我曾尝试将资源类型设置为DesignDataDesignDataWithDesignTimeCreatableTypes,但没有运气。

回答

2

它应该是:

<Label FontSize="8" Content="{Binding Path=PersonList.Count}"/> 

而且貂是正确的,你应该使用一个ObservableCollection代替。

HTH

+0

这工作,非常感谢! – CityView 2011-04-02 17:22:35

+0

没有问题的人,只是接受答案;) – Marcote 2011-04-02 17:25:32

+0

我不得不等待整个晚上36秒,有这么多的警告,使用这个网站,如果你是新的。 – CityView 2011-04-02 17:27:06

1

它应该工作,但成为一次性绑定,因为您的列表不执行INotifyPropertyChanged,因此绑定在Count更改时不会更新。

尝试使用ObservableCollection<Person>代替。

+1

感谢您的快速回复,我只是给它一个去,不幸的是这也不起作用。我应该补充说,并不是我得到'0'显示,绑定完全失败,并且根本没有文本显示。我不知道如何在设计器视图中调试绑定失败... – CityView 2011-04-02 17:18:02

+0

啊,对不起,我看错了你的代码。绑定应该是:{绑定路径= PersonList.Count} – 2011-04-02 17:23:38

2

市景,就像一个侧面说明:调试数据绑定我通常使用一个空的转换器,只返回它被赋予的价值。我在那里放置了一个断点,这样我就可以看到来回发生了什么。

public class BindTestConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value; 
    } 
} 

与此相结合,输出窗口告诉我什么通常导致我解决手头上的问题。