All, I have some test code that works with some test data defined as
private ObservableCollection<TestClass> _testData = new ObservableCollection<TestClass>();
public ObservableCollection<TestClass> TestData
{
get { return _testData; }
set { _testData = value; }
}
and is bound at design-time via
<DataGrid x:Name="dataGrid" ItemsSource="{Binding TestData}".../>
I have created a DataGrid
which gets populated at run-time via
dataGrid.ItemsSource = BuildDataGridColumns(cultureDict).Tables[0].AsDataView();
with
private DataSet BuildDataGridColumns(Dictionary<string, string> cultureDict,
DataTable additionalDt = null)
{
// ...
}
Which works great, however the code that updates the grids visuals that used to work with the test data no longer does due to (I believe) ItemsSource="{Binding TestData}"
being absent. The XAML that is supposed to bind the text inserted into a TextBox
and that in the cells of the DataGrid
is:
<DataGrid x:Name="dataGrid"
local:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}"
AlternatingRowBackground="Gainsboro" AlternationCount="2" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<DataGrid.Resources>
<local:SearchValueConverter x:Key="searchValueConverter" />
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="local:DataGridTextSearch.IsTextMatch">
<Setter.Value>
<MultiBinding Converter="{StaticResource searchValueConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" />
<Binding RelativeSource="{RelativeSource Self}" Path="(local:DataGridTextSearch.SearchValue)" />
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="local:DataGridTextSearch.IsTextMatch" Value="True">
<Setter Property="Background" Value="Orange" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF007ACC"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
My question is;
How can I create the same binding of my
DataGrid
to the data set I create at run-time?
Someone has suggested that the reason is that my data does not implement INotifyCollectionChanged
interface, but due to the variable nature of the data, I have to load the data into the DataGrid
at run-time.
How can I bind this data to the
DataGrid
as a class that implementsINotifyPropertyChanged
if the structure of the data can change?
Thanks very much for your time.