簡體   English   中英

UserControl 中的 wpf 綁定集合屬性

[英]wpf binding collection property in UserControl

我有一個自定義 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(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 標記中,我可以像這樣定義我的控件

    <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),
        typeof(FormQuestionReportViewer), 
        new PropertyMetadata(null));

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

其次,您需要一個常規的 CLR 屬性,例如ObservableCollection<Question> Questions ,您可以向其中顯式添加項目:

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>

當您想要獲得完整的項目列表時,額外的工作就來了。 您需要將兩個集合合並為一個集合。 這個統一的集合將作為第三個屬性公開,它返回一個只讀集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM