c# - UserControl 中的 wpf 绑定(bind)集合属性

标签 c# wpf xaml

我有一个自定义 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 中绑定(bind)我的集合属性

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

我如何进行绑定(bind)?

最佳答案

您将不得不公开两个单独的属性,很像 ItemsControl它有一个 ItemsItemsSource属性(property)。看起来您希望能够使用绑定(bind) and 通过添加到您的集合来显式添加项目。此行为与 ItemsControl 不同,ItemsControl 仅允许您使用 Items 或 ItemsSource 属性,但不能同时使用两者。

虽然没有什么可以阻止您添加对指定项目的两种方式的支持,但您需要做更多的工作。

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

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>

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

关于c# - UserControl 中的 wpf 绑定(bind)集合属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7321710/

相关文章:

c# - C#程序的自动更新

c# - C#中整数相减时错误 "Can' t implicitly convert int to short"

c# - 调试 DLL 的发布版本(使用 PDB 文件)

C# DateTime 从 yyyy-MM-ddTHH :mm:ss to dd MMM yyyy 转换

c# - DispatcherTimer 在更新 UI 时每分钟变慢

c# - 将 WPF 绑定(bind)源设置为 DataGrid 列

wpf - 谁将 listviewcolumn 标题绑定(bind)到 WPF 中的列单元格

wpf - 在 WPF 控件之间添加空格

c# - 在 Windows 10 通用应用程序中停止播放页面上的所有媒体元素

c# - Windows Phone 8 即使手机的主题发生变化,如何始终保持一个主题