微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – UserControl中的wpf绑定集合属性

我有一个自定义UserControl,其中包含自定义对象的集合.
public class Question : FrameworkElement
{
    public readonly static DependencyProperty FullNameProperty =
        DependencyProperty.Register("FullName",typeof(string),typeof(Question));

    public readonly static DependencyProperty ShortNameProperty =
        DependencyProperty.Register("ShortName",typeof(Question));

    public readonly static DependencyProperty RecOrderProperty =
        DependencyProperty.Register("RecOrder",typeof(int),typeof(Question));

    public readonly static DependencyProperty AnswerProperty =
        DependencyProperty.Register("Answer",typeof(Question));

    public string FullName
    {
        get { return (string)GetValue(FullNameProperty); }
        set { SetValue(NameProperty,value); }
    }

    public string ShortName
    {
        get { return (string)GetValue(ShortNameProperty); }
        set { SetValue(ShortNameProperty,value); }
    }

    public string Answer
    {
        get { return (string)GetValue(AnswerProperty); }
        set { SetValue(AnswerProperty,value); }
    }

    public int RecOrder
    {
        get { return (int)GetValue(RecOrderProperty); }
        set { SetValue(RecOrderProperty,value); }
    }
}

在我的控制代码后面,我有

public readonly static DependencyProperty Questionsproperty =
        DependencyProperty.Register("Questions",typeof(ObservableCollection<Question>),typeof(FormQuestionReportViewer),new PropertyMetadata(new ObservableCollection<Question>()));


     public ObservableCollection<Question> Questions
     {
        get { return GetValue(Questionsproperty) as ObservableCollection<Question>; }
        set { SetValue(Questionsproperty,value); }
     }

在xaml标记中,我可以像这样定义我的控件

<custom:CustomControl>
        <custom:CustomControl.Questions>
            <custom:Question FullName="smth text" ShortName="smth text" RecOrder="1" Answer="Yes" />
            <custom:Question FullName="smth text" ShortName="smth text" RecOrder="2" Answer="Yes" />
        </custom:CustomControl.Questions>          
    </custom:CustomControl>

它运行良好,但我想在xaml中绑定我的集合属性,就像这样

<custom:CustomControl>
        <custom:CustomControl.Questions Items="{binding Path=Questions}">
            <custom:Question FullName="{binding Name}" ShortName="{binding ShortName}" RecOrder="{binding RecOrder}" Answer={binding Answer}" /> 
        </custom:CustomControl.Questions>          
    </custom:CustomControl>

我怎么能做这个约束?

解决方法

您必须公开两个单独的属性,就像具有 ItemsItemsSource属性的ItemsControl一样.看起来您希望能够使用绑定添加项目,并通过添加到您的集合中进行显式添加.此行为与ItemsControl不同,后者仅允许您使用Items或ItemsSource属性,但不能同时使用两者.

没有什么可以阻止您添加对指定项目的两种方式的支持,但是对您来说这将是更多的工作.

首先,您需要一个DependencyProperty,例如IEnumerable QuestionsSource,您可以绑定到:

public readonly static DependencyProperty QuestionsSourceProperty =
    DependencyProperty.Register("QuestionsSource",typeof(IEnumerable),new PropertyMetadata(null));

 public IEnumerable QuestionsSource
 {
    get { return GetValue(QuestionsSourceProperty) as IEnumerable; }
    set { SetValue(QuestionsSourceProperty,value); }
 }

第二,你需要一个常规的CLR属性,比如ObservableCollection< Question>问题,您可以明确地添加项目:

private ObservableCollection<Question> questions = new ObservableCollection<Question>();
public ObservableCollection<Question> Questions
 {
    get { return questions; }
 }

然后你可以像这样使用这些属性

<custom:CustomControl QuestionsSource="{Binding Path=Questions}">
    <custom:CustomControl.Questions>
        <custom:Question FullName="{binding Name}" ShortName="{binding ShortName}" RecOrder="{binding RecOrder}" Answer={binding Answer}" /> 
    </custom:CustomControl.Questions>          
</custom:CustomControl>

当您想要获得完整的项目列表时,需要额外的工作.您需要将两个集合合并为一个集合.此统一集合将作为第三个属性公开,它返回只读集合.

原文地址:https://www.jb51.cc/csharp/238998.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐