How to bind collection to WPF:DataGridComboBoxColumn

后端 未结 3 830
無奈伤痛
無奈伤痛 2020-12-17 09:27

I have a simple object like:

class Item
{
  ....

  public String Measure { get; set; }
  public String[] Measures {get; }
}

Which I am try

相关标签:
3条回答
  • 2020-12-17 10:03

    This is hands down the best solution:

    http://wpfthoughts.blogspot.com/2015/04/cannot-find-governing-frameworkelement.html

    The idea here is that you declare a CollectionViewSource as a static resource and then declaratively bind it to ItemsSource of the DataGridComboBoxColumn.

    Create and bind a static CollectionViewSource:

     <Page.Resources>
         <CollectionViewSource x:Key="Owners" Source="{Binding Owners}"/>
     </Page.Resources>
    

    And then bind your target ItemsSource:

    ItemsSource="{Binding Source={StaticResource Owners}}"
    
    0 讨论(0)
  • 2020-12-17 10:06

    The problem lies in that Columns does no inherit DataContext.

    See more here Binding in a WPF data grid text column

    here blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx

    and here http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

    0 讨论(0)
  • 2020-12-17 10:08

    If your measures are common for all objects, then you can make measures static

    public String[] Measures { get; }
    

    And your xaml will use it as it's shown below:

    <DataGridComboBoxColumn
        Header="Role"
        SelectedValueBinding="{Binding Role}"
        ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
        DisplayMemberPath="Name"/>
    

    Hopefully, it will help.

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