天天看點

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>
           

繼續閱讀