繁体   English   中英

C#/ WPF-DesignData-绑定到DesignData集合属性

[英]C# / WPF - DesignData - Binding to DesignData Collection Properties

我喜欢设计时间数据,尤其是在创建小部件时。 对于这个非常简单的用例,我很难绑定到我在xaml中创建的设计时列表的属性。

请在下面找到我的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以便绑定到其中指定的列表的Count?

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

作为一个补充说明,CityView:调试DataBinding我通常使用一个空转换器,该转换器仅返回给定的值。 我在其中放置了一个断点,这样我就可以看到确切的来回变化。

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

结合这些以及“输出”窗口告诉我的内容,通常可以使我找到当前问题的解决方案。

它应该是:

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

同样Mårten是正确的,您应该改用ObservableCollection。

HTH

它应该可以工作,但是成为一次性绑定,因为您的列表没有实现INotifyPropertyChanged ,因此当Count更改时,绑定不会更新。

尝试改为使用ObservableCollection<Person>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM