How to bind an ItemsSource to a private property

前端 未结 4 1155
情书的邮戳
情书的邮戳 2021-01-11 19:25

How to bind WPF an ItemsSource to a private property?



        
相关标签:
4条回答
  • 2021-01-11 20:01

    You need a Relative Source binding, right now your binding is to the DataContext of your ItemBuySellAddEdit (FrameworkElement) Atleast that is my Impression, because you are using partial. If it is an ViewModel check the Output Window, and look if you have any binding errors.

    <ComboBox 
        x:Name="xxx" 
        ItemsSource="{Binding Items, 
            RelativeSource={RelativeSource AncestorType={x:Type ItemBuySellAddEdit}},
            Mode=OneWay}"
        DisplayMemberPath="ItemName"/>
    

    But the Answer from Stephan Bauer still applies.

    Also Take the answer from WaltiD into consideration if you want new items in that list to show up automatically.

    0 讨论(0)
  • 2021-01-11 20:03

    If you really really wanted to do this, you would have to provide a custom type descriptor, by implementing ICustomTypeDescriptor - that provides the extra property via a custom PropertyDescriptor alongisde the regular public properties. You can implement this interface on the type itself, or via TypeDescriptionProvider; the latter is preferred, as it works in more scenarios (things like empty lists, without needing to also provide a custom list with an ITypedList implementation). This is a lot of work, and it really isn't worth it except in extreme cases. But it can be done.

    0 讨论(0)
  • 2021-01-11 20:07

    This is not possible, if you'd like, you could use internal instead.

    ... and use ObservableCollection<T> and don't forget to set the DataContext of the view.

    0 讨论(0)
  • 2021-01-11 20:11

    DataBinding in WPF works only with public properties.

    MSDN:

    The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation

    0 讨论(0)
提交回复
热议问题