WPF在用户控件绑定集合属性 [英] wpf binding collection property in UserControl

查看:214
本文介绍了WPF在用户控件绑定集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户控件,其中包含自定义对象的集合

I have a custom UserControl, which contains collection of custom objects.

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(string), typeof(Question));

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

    public readonly static DependencyProperty AnswerProperty =
        DependencyProperty.Register("Answer", typeof(string), 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标记我可以定义我的控制这样的

And in xaml markup I can define my control like this

    <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绑定我的集合属性。

It's works well, but I want to make binding my collection property in xaml like this

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

我怎么可以让有约束力?

How I can make that binding?

推荐答案

您必须公开两个独立的特性,很像一个ItemsControl其中有一个的产品和的的ItemsSource 的属性,它看起来像你希望能够有约束力的明确将添加使用的项目到您的收藏。这种行为会从一个ItemsControl,只允许您使用在同一时间的项目或ItemsSource属性,但不能两者都不同。

You would have to expose two separate properties, much like an ItemsControl which has an Items and ItemsSource property. It looks like you want to be able to add items using a binding and explicitly by adding to your collection. This behavior would differ from an ItemsControl, which only allows you to use the Items or ItemsSource property, but not both at the same time.

有什么防止你的加入虽然指定项目的两种方式的支持,但它会在你的一部分,更多的工作。

There is nothing preventing you from adding support for both ways of specifying items though, but it would be more work on your part.

首先,你需要一个的DependencyProperty,如的IEnumerable QuestionsSource ,它可以绑定到:

First, you'd need a DependencyProperty, such as IEnumerable QuestionsSource, which you could bind to:

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

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



的Econd,你需要一个普通的CLR属性,如的ObservableCollection<问题>问题,您可以添加项目明确:

econd, you'd need a regular CLR property, such as ObservableCollection<Question> Questions, which you could add items to explicitly:

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



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

Then you could use these properties like so:

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

额外的工作来如果你想获得项目的完整列表。你需要工会两个集合到一个单一的集合。这个统一收集将被公开为第三个属性,它返回一个只读集合。

The extra work comes when you want to get the full list of items. You'd need to union the two collections into a single collection. This unified collection would be exposed as a third property, which returns a read-only collection.

这篇关于WPF在用户控件绑定集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆