天天看点

DataGrid中的ComboBox独自绑定

DataGrid中有个ComboBox他需要个独立的ItemsSource需要绑定,他不是在DataGrid的ItemsSource下的item中的属性,而是与ItemsSource平级的,那么就要独自绑定DataContext,参考的当不继承DataContext时如何绑定到数据

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
           
<DataGrid>
    <DataGrid.Resources>
        <l:BindingProxy x:Key="proxy" Data="{Binding}" />
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridComboBoxColumn ItemsSource="{Binding Path=Data.Results,Source={StaticResource proxy}}"/>
    </DataGrid.Columns>
</DataGrid>
           

继续阅读