I have a Collection of Values in an Array of Strings, or in a List or whatever, and what i want is to set this Collection of values as the Collection of items into all the ComboBoxes in a column as DataGridComboBoxColumn in WPF.
I think i don't have other way to access this Collection of Values and bind it equally to all the ComboBoxes (in XAML) beside the DataContext. But can i access the DataContext of a DataGrid from a DatGridComboBoxColumn (in XAML)? Can i do it? How?
How i specify (in XAML) in DatagridComboBoxColumn to put this Collection of Items equally in all ComboBoxes? How i can achieve this?
Here is my XAML:
(...)xmlns:local="clr-namespace:WpfApp"(...)
<Grid Name="grid1">
<DataGrid Name="dataGrid" AutoGenerateColumns="True">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Options" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
And here is my Code Behind:
public class TestClass
{
private static string[] stringArray = { "Option One", "Option Two", "Option Three" };
public static string[] StringArray
{
get
{
return stringArray;
}
}
}
But can i access the DataContext of a DataGrid from a DatGridComboBox Column (in XAML)? Can i do it? How?
There's no straightforward way to access the DataContext
of the DataGrid
from the column definition, because it isn't part of the visual or logical tree, so it doesn't inherit the DataContext. I recently blogged about a solution to that issue. Basically you need to do something like that:
<DataGrid Name="dataGrid" AutoGenerateColumns="True">
<DataGrid.Resources>
<!-- Proxy for the current DataContext -->
<local:BindingProxy x:Key="proxy" Data="{Binding"} />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Options" Width="100"
ItemsSource="{Binding Source="{StaticResource proxy}", Path=Data}"/>
</DataGrid.Columns>
</DataGrid>
BindingProxy
is a simple class that derives from Freezable
and exposes a Data
dependency property.