WPF Xaml converter Error - wpf

I am getting a compilation error as
Error 1 The type or namespace name 'Converters' could not be found (are you missing a using directive or an assembly reference?) G:\C#\Practice\DataGrid\DataGrid\obj\x86\Debug\MainWindow.g.cs 12 7 DataGrid
Below is my Xaml code, I am new to WPF please help me out. I have the converter Class.
<Window x:Class="DataGrid.MainWindow "
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="500" Width="700"
xmlns:c="clr-namespace:Converters">
<Window.Resources>
<c:BoolToStringConverter x:Key="BoolToStringConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="59*" />
<RowDefinition Height="402*" />
</Grid.RowDefinitions>
<StackPanel Margin="0,55,0,0" Grid.RowSpan="2">
<DataGrid ItemsSource="{Binding Path=Courses}" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="datagrid1" CanUserAddRows="False" HeadersVisibility="Column" RowDetailsVisibilityMode="Visible" VerticalScrollBarVisibility="Auto" CanUserSortColumns="True" CanUserResizeColumns="False" Height="339" Width="610">
<DataGrid.Resources>
<Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Margin" Value="4" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Course Title" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Code}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Course Description" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Descrption}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Required" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=IsRequired, Converter={StaticResource BoolToStringConverter}}"
VerticalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
<StackPanel Margin="0,0,539,32">
<TextBlock Text="Select Enroolment:" FontSize="15"
Height="26"
Grid.RowSpan="1"
Width="134">
</TextBlock>
</StackPanel>
<TextBlock DockPanel.Dock="Left"
HorizontalAlignment="Left"
Text="Select Course:"
Width="139" FontSize="15"
Margin="0,32,0,0">
</TextBlock>
<ComboBox HorizontalAlignment="Right"
Margin="0,0,69,33"
Name="comboBox1"
Width="476"
Height="23"
VerticalAlignment="Bottom" />
</Grid>

xmlns:c="clr-namespace:Converters"
You should check the namespace of your Converters class, that is what you should put in place of Converters. If Converters is your class, then you should put whatever it says after namespace in your .cs file.
Example:
namepsace MyNamespace {
public class Converters { }
}
Then your XAML would be:
xmlns:c="clr-namespace:MyNamespace"
Extra:
If you want to be more specific, you can add the assembly parameter as well.
xmlns:window="clr-namespace:MyNamespace;assembly=MyNamespace"
Where Assembly can be found in your Build Properties (Right-click Project, Properties, Application, Assembly Information)
Your default namespace can also be found in the Build Properties as well.

Related

Bind to property in a relative source sends me up to the mainwindow + Catel

Have the following xaml code
<catel:UserControl x:Class="SICUAP.Views.CatProducto_CategoriasView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:catel="http://catel.codeplex.com"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Views="clr-namespace:SICUAP.Views">
<!-- Resources -->
<UserControl.Resources>
</UserControl.Resources>
<!-- Content -->
<catel:StackGrid>
<catel:StackGrid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</catel:StackGrid.RowDefinitions>
<Views:cmdGlobalesBDView></Views:cmdGlobalesBDView>
<Label Content="Catalogo de Categorias de Producto" Style="{StaticResource estiloTituloControl}">
</Label>
<DataGrid Margin="0 10 0 0" MaxHeight="200" ItemsSource="{Binding Producto_Categorias}" SelectedItem="{Binding SelectedProducto_Categoria}" AutoGenerateColumns="False"
ScrollViewer.VerticalScrollBarVisibility="Auto" CanUserAddRows="False" CanUserResizeColumns="False" AlternatingRowBackground="#D2EDF7">
<DataGrid.Columns>
<DataGridTemplateColumn Header="CLAVE" MinWidth="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding ID_CATEGORIA}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="NOMBRE" MinWidth="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding NOMBRE}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="DESCRIPCION" MinWidth="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding DESCR}" HorizontalAlignment="Stretch"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
<Views:dataProducto_CategoriasView DataContext="{Binding SelectedProducto_Categoria}" Visibility="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.VistaDetalleEsVisible, Converter={StaticResource booleanToVisibilityConverter}}" />
</catel:StackGrid>
</catel:UserControl>
When i try to bind the Visibility property of the internal view
<Views:dataProducto_CategoriasView DataContext="{Binding SelectedProducto_Categoria}" Visibility="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.VistaDetalleEsVisible, Converter={StaticResource booleanToVisibilityConverter}}" />
to the parent it sends me all the way up to the datacontext of the main window.
The parent is loaded in a contentControl inside the main window.
Why i cant bind to the datacontext of the parent user control?
It's exactly the same question as this one.
Read the docs, specifically the article UserControl - Under the hood.

How to Increase the Height of Silverlight Datagrid Header

I am new to silver Light. How to Increase the Height of Silverlight Datagrid Header
I am using template column
Code as follows :
<sdk:DataGridTemplateColumn Header="Base Receipt">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<sdk:Label Margin="5,0,5,0" Height="25" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn>
You can use HeaderStyle :
<sdk:DataGridTemplateColumn Header="Base Receipt">
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="Height" Value="50" />
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<sdk:Label Margin="5,0,5,0" Height="25" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn>

WPF TextBlock StringFormat Binding To Parent

I have been trying to bind the StringFormat of the Text property of a TextBlock to a templated parent.
Here is where I'm trying to set the StringFormat:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataFlowControls">
<Style TargetType="{x:Type local:DfcEditTextBox}">
<Setter Property="Margin" Value="-6, 0, -6, 0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DfcEditTextBox}">
<TextBlock x:Name="PART_TextBlock"
Padding="2, 0, 0, 0"
Text="{Binding Path=Value, StringFormat=ThisStringFormat, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Here is the parent:
<Window x:Class="DataFlowControls.Show.DfcListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dfc="clr-namespace:DataFlowControls;assembly=DataFlowControls"
xmlns:local="clr-namespace:DataFlowControls.Show"
Title="DfcListView" Height="400" Width="500">
<Grid>
<StackPanel>
<dfc:DfcListView Name="lvTradesCollection" ItemsSource="{Binding Path=TradesCollection}" KeyboardNavigation.DirectionalNavigation="Continue" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<DataTemplate x:Key="Date_DataTemplate">
<dfc:DfcEditTextBox Value="{Binding Path=Date, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ThisStringFormat='{}{0:dd/MM/yyyy}' HorizontalContentAlignment="Left" IsEditable="true" />
</DataTemplate>
<DataTemplate x:Key="Asset_DataTemplate">
<dfc:DfcEditTextBox Value="{Binding Path=Asset, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Left" IsEditable="true" />
</DataTemplate>
<DataTemplate x:Key="Lots_DataTemplate">
<dfc:DfcEditTextBox Value="{Binding Path=Lots, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ThisStringFormat='N2' HorizontalContentAlignment="Center" IsEditable="true" />
</DataTemplate>
<DataTemplate x:Key="Price_DataTemplate">
<dfc:DfcEditTextBox Value="{Binding Path=Price, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Center" IsEditable="true" />
</DataTemplate>
<DataTemplate x:Key="IsCheap_DataTemplate">
<dfc:DfcEditCheckBox Value="{Binding Path=IsCheap, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" IsEditable="true" />
</DataTemplate>
<DataTemplate x:Key="NextTrade_DataTemplate">
<dfc:DfcEditComboBox Value="{Binding Path=NextTrade, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{x:Static local:DfcListView.NextTradeTypes}" IsEditable="true" />
</DataTemplate>
</ListView.Resources>
<ListView.View>
<dfc:DfcGridView>
<dfc:DfcGridViewColumn Header="Date" Width="140" CellTemplate="{StaticResource Date_DataTemplate}" />
<dfc:DfcGridViewColumn Header="Asset" Width="40" CellTemplate="{StaticResource Asset_DataTemplate}" />
<dfc:DfcGridViewColumn Header="Lots" Width="40" CellTemplate="{StaticResource Lots_DataTemplate}" />
<dfc:DfcGridViewColumn Header="Price" Width="50" CellTemplate="{StaticResource Price_DataTemplate}" />
<dfc:DfcGridViewColumn Header="IsCheap" Width="60" CellTemplate="{StaticResource IsCheap_DataTemplate}" />
<dfc:DfcGridViewColumn Header="NextTrade" Width="80" CellTemplate="{StaticResource NextTrade_DataTemplate}" />
</dfc:DfcGridView>
</ListView.View>
</dfc:DfcListView>
<Button Content="Add Row" HorizontalAlignment="Left" Margin="5,5,5,5" Click="AddRow_Click"/>
<Button Content="Update Row" HorizontalAlignment="Left" Margin="5,5,5,5" Click="UpdateRow_Click"/>
</StackPanel>
</Grid>
</Window>
Everything works fine until I include the StringFormat=ThisStringFormat, which mucks it up. But I somehow need to connect the StringFormat to the ThisStringFormat property expressed in the parent. I've experimented with changing StringFormat=ThisStringFormat to try to get to the templated parent, but to no avail.
Any ideas on how to solve this one?
The StringFormat property is just a regular property on BindingBase and regular properties cannot be binding targets only dependency properties can. So the answer is: You cannot not do it that way.
Some possible approaches:
Subclass TextBox and add a string format dependency property to which you can bind providing the required functionality
Augment your view model (if you have one) with a FormattedValue property (probably a bit ugly)
Use a MultiBinding for the Text property. One binding goes to Value and one to the ThisStringFormat of the templated parent. Then write a converter implementing IMultiValueConverter to return the formatted value.

Changing ContentTemplate based on ListBox selection

I have a Listbox and a Border in a StackPanel similar to the following:
<StackPanel Orientation="Horizontal">
<ListBox>
<ListBoxItem Content="People"/>
<ListBoxItem Content="Animals"/>
<ListBoxItem Content="Cars"/>
</ListBox>
<Border Width="200>
<ContentPresenter/>
</Border>
</StackPanel>
When selecting an item in the listbox I would like to change the content in the ContentPresenter accordingly e.g. selecting People would change the template to display a series of input fields related to people where as selecting Animals would display a series of fields related to Animals etc. - the behavior of this would be akin to a TabControl.
I think I can achieve this with a DataTrigger which changes the DataTemplate in the Border but I'm not sure how to achieve this.
Any pointers?
Thanks
You can toggle the ContentTemplate using a DataTrigger as follows.
Note, that I am binding an ObservableCollection to a simple object (Thing) with one property called Name, and am I binding the Content of the ContentControl to the SelectedItem in the ListBox using a ViewModel.
<Grid>
<Grid.Resources>
<local:MultiValueConverter x:Key="con" />
<DataTemplate x:Key="PeopleTemplate">
<StackPanel Orientation="Horizontal">
<Label Margin="0,0,5,0" Content="People Name" HorizontalAlignment="Left" Grid.Column="0" />
<TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
<Button Content="OK" Grid.Column="2" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="AnimalsTemplate">
<StackPanel Orientation="Horizontal">
<Label Margin="0,0,5,0" Content="Animal Name" HorizontalAlignment="Left" Grid.Column="0" />
<TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
<Button Content="OK" Grid.Column="2" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="CarsTemplate">
<StackPanel Orientation="Horizontal">
<Label Margin="0,0,5,0" Content="Car Name" HorizontalAlignment="Left" Grid.Column="0" />
<TextBox Grid.Column="1" Width="100" Height="25"></TextBox>
<Button Content="OK" Grid.Column="2" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<ListBox ItemsSource="{Binding Things}" SelectedItem="{Binding SelectedThing}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0" Orientation="Horizontal">
<TextBlock Padding="5" Text="{Binding Name}" Margin="0"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border Width="200">
<ContentControl Content="{Binding SelectedThing}">
<ContentControl.ContentTemplate>
<DataTemplate>
<ContentControl Name="cc"
Content="{Binding}"
ContentTemplate="{StaticResource PeopleTemplate}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Name}" Value="People">
<Setter TargetName="cc"
Property="ContentTemplate"
Value="{StaticResource PeopleTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Name}" Value="Animals">
<Setter TargetName="cc"
Property="ContentTemplate"
Value="{StaticResource AnimalsTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Name}" Value="Cars">
<Setter TargetName="cc"
Property="ContentTemplate"
Value="{StaticResource CarsTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
</StackPanel>
<Grid>
Here is the Thing class:
public class Thing
{
public Thing(String name)
{
this.Name = name;
}
public String Name { get; set; }
public static ObservableCollection<Thing> GetThingList()
{
return new ObservableCollection<Thing>(new Thing[3] {
new Thing("People"),
new Thing("Animals"),
new Thing("Cars")
});
}
}

WPF DataGrid Grouping does not show up due to binding error

I get this error message:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Windows.Data.ListCollectionView' BindingExpression:Path=MaterialList; DataItem='MaterialBrowserListViewModel' (HashCode=24964411); target element is 'CollectionViewSource' (HashCode=36518048); target property is 'Source' (type 'Object')
Below is the important Xaml + ViewModel code.
What is wrong with my binding?
VIEWMODEL:
public class MaterialBrowserListViewModel : ViewModelBase
{
private IDocumentRepository _docRepo;
private ICollectionView _materialList;
public MaterialBrowserListViewModel()
{
_docRepo= new DocumentRepository();
MaterialList = CollectionViewSource.GetDefaultView(_docRepo.GetMaterialList());
//_materialList.GroupDescriptions.Add(new PropertyGroupDescription("Schoolclasscode"));
}
public ICollectionView MaterialList
{
get { return _materialList; }
set
{
_materialList = value;
this.RaisePropertyChanged("MaterialList");
}
}
}
VIEW:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--<ResourceDictionary Source="Themes\DataGrid.Generic.xaml"/>-->
</ResourceDictionary.MergedDictionaries>
<CollectionViewSource Source="{Binding MaterialList}" x:Key="groupedView">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DocumentName"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<!-- GroupHeaderStyle -->
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True"
Background="Blue"
Foreground="White">
<Expander.Header>
<TextBlock Text="{Binding Name.Name}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Margin="0,0,0,10" Grid.Row="0" Orientation="Horizontal">
<Button Content="Open" />
<Button Content="Delete" />
<Button Content="Export" />
<Button Content="Clear Filter" />
</StackPanel>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=col0, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=col2, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=col3, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=col4, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=col5, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<DatePicker Grid.Column="0" />
<TextBox Grid.Column="1" />
<ComboBox Grid.Column="2" />
<ComboBox Grid.Column="3" />
<TextBox Grid.Column="4" />
</Grid>
<DataGrid
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding Source={StaticResource groupedView}}"
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="15"
x:Name="MaterialGrid"
IsSynchronizedWithCurrentItem="True"
AlternatingRowBackground="AliceBlue"
VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingStackPanel.IsVirtualizing="True"
HeadersVisibility="Column"
CanUserResizeColumns="True"
CanUserSortColumns="True"
IsReadOnly="True"
>
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Schoolday}" x:Name="col0" Header="Date" />
<DataGridTextColumn Binding="{Binding Period}" x:Name="col1" Header="Period" />
<DataGridTextColumn Binding="{Binding SchoolclassCode}" x:Name="col2" Header="Class code" />
<DataGridTextColumn Binding="{Binding DocumentName}" x:Name="col3" Header="Document name" />
<DataGridTextColumn Binding="{Binding Keywords}" x:Name="col4" Header="Keywords" />
<DataGridTextColumn Binding="{Binding DocumentId}" x:Name="col5" Header="Doc Id" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
Update: Why is the Binding not working for "Schoolclasscode" below?
Error: System.Windows.Data Error: 40 : BindingExpression path error: 'SchoolclassCode' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=15576908)'. BindingExpression:Path=SchoolclassCode; DataItem='CollectionViewGroupInternal' (HashCode=15576908); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
<!-- GroupHeaderStyle -->
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True"
Background="AliceBlue"
Foreground="White">
<Expander.Header>
<TextBlock Text="{Binding SchoolclassCode}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have changed the binding now to the current datacontext to see whats in there:
<DataGrid
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding Source={StaticResource ResourceKey=groupedView}}"
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="15"
x:Name="MaterialGrid"
IsSynchronizedWithCurrentItem="True"
AlternatingRowBackground="AliceBlue"
VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingStackPanel.IsVirtualizing="True"
HeadersVisibility="Column"
CanUserResizeColumns="True"
CanUserSortColumns="True"
IsReadOnly="True"
>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock Foreground="Black" Text="{Binding }"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Schoolday}" x:Name="col0" Header="Date" />
<DataGridTextColumn Binding="{Binding Period}" x:Name="col1" Header="Period" />
<DataGridTextColumn Binding="{Binding SchoolclassCode}" x:Name="col2" Header="Class code" />
<DataGridTextColumn Binding="{Binding DocumentName}" x:Name="col3" Header="Document name" />
<DataGridTextColumn Binding="{Binding Keywords}" x:Name="col4" Width="*" Header="Keywords" />
<!--<DataGridTextColumn Binding="{Binding DocumentId}" x:Name="col5" Header="Doc Id" />-->
</DataGrid.Columns>
</DataGrid>
Do you see the class name at the Expander Text ? How can I access my ObservableCollection from this point?
First of all, I would have made this a comment on Elisa's resolution about CollectionViewGroup.Name - if I had points enough to do so. Got this error too on my first attempt to use grouping.
The population of this Name property, from the source property beneath, is set by the use of:
_materialList.GroupDescriptions.Add(new PropertyGroupDescription("Schoolclasscode"));
(More than one PropertyGroupDescription added to one view causes additional group styles to be applied for each and their corresponding Name properties populated accordingly.)
To clarify the Elisa's resolution - just change the binding as below:
<Expander IsExpanded="True" Background="AliceBlue" Foreground="White">
<Expander.Header>
<TextBlock Text="{Binding Name}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
Hope this saves could save time for somebody else.
A CollectionViewSource takes a collection and wraps it with an ICollectionView. However, You are binding directly to a ICollectionView, which it cannot wrap. Make your property in the model some raw collection type IEnumerable) and bind to it.
Here's the code to CollectionViewSource's IsSourceValid:
private static bool IsSourceValid(object o)
{
if (((o != null) && !(o is IEnumerable)) && (!(o is IListSource) && !(o is DataSourceProvider)))
{
return false;
}
return !(o is ICollectionView);
}
You can see it specifically checks for a ICollectionView and disallows it. Even though ICollectionView is IEnumerable, it is still not allowed.

Resources