0
votes

I have the ItemsSource property of a DataGrid bind to a property of my ViewModel!

<DataGrid ItemsSource="{Binding Path=ExcelData}" ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" Grid.Row="1" Margin="0 10 0 10" Visibility="{Binding DisplayGridView, Converter={StaticResource booltovisibility}}" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" />

As you can see, the DataGrid has a special header template.

<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Background" Value="{x:Static pm:MetroColors.FeatureBrush}" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridColumnHeader">
                    <Border BorderThickness="2" CornerRadius="5" Background="{x:Static pm:MetroColors.FeatureBrush}" BorderBrush="{x:Static pm:MetroColors.FeatureBrush}">
                        <StackPanel>
                            <ComboBox ItemsSource="{Binding ComboDataSource}" DisplayMemberPath="Text" SelectedValuePath="Name" ext:ComboBoxExtensions.ComboBoxName="{TemplateBinding Content}" SelectionChanged="ComboBox_SelectionChanged" />
                            <Label Content="{TemplateBinding  Content}" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Problem : the combobox is not filled.

I have this output :

System.Windows.Data Error: 40 : BindingExpression path error: 'ComboDataSource' property not found on 'object' ''String' (HashCode=752763509)'. BindingExpression:Path=ComboDataSource; DataItem='String' (HashCode=752763509); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

I am agree with it! there is no property named ComboDataSource on the object binded to the columnheader.

How can I bind my combobox to a different source?

1

1 Answers

1
votes

You could find another appropriate Source by walking the tree, for that you can use the RelativeSource markup with FindAncestor like this

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.ComboDataSource}"

This of course implies that there is a DataGrid in the ancestors of the ComboBox and that the DataGrids DataContext is set and it contains a property called ComboDataSource.

Another solution i like for static data in comboboxes is to set a new source for the binding

ItemsSource = "{Binding Source={x:Static my:StaticData.MyEntries}}"

if you use a singleton manager you can do this

ItemsSource = "{Binding Source={x:Static my:StaticData.Instance}, Path=MyEntries}"

And finally you can use a DataObjectProvider, while i never really used it myself, but this should explain what you can do with it.