Reputation: 454
I have a class that contains a subclass
public class CustomerDate
{
public string Date { set; get; }
public Customerdetails _Customerdetails { set; get; }
public CustomerDate()
{
_Customerdetails = new Customerdetails();
}
}
public class Customerdetails
{
public int Id { set; get; }
public string Name { set; get; }
public Customerdetails() { }
}
I Have a list of CustomerDate Objects that I want to bind to a listview grouped by subClass(Customerdetails). My problem is that I can show SubClass property in listview's Gridview ({Binding Customerdetails.Name}) but I cannot show SubClass details in GroupStyle section.
<TextBlock Text="{Binding Path=Customerdetails.Name}" />
Not working But
<GridViewColumn DisplayMemberBinding="{Binding Customerdetails.Name}"
is working.
Any Idea?
List<CustomerDate> CustomerDateList = new List<CustomerDate>();
.
.
.
lv.ItemsSource = DBAccess.GetBadMonthlyPaymentCustomers();
CollectionView view =
(CollectionView)CollectionViewSource.GetDefaultView(lv.ItemsSource);
PropertyGroupDescription gd = new PropertyGroupDescription("Customerdetails");
view.GroupDescriptions.Add(gd);
<ListView x:Name="lv">
<ListView.View>
<GridView x:Name="GridView" >
<GridViewColumn DisplayMemberBinding="{Binding Date}" />
<GridViewColumn DisplayMemberBinding="{Binding Customerdetails.Name}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Text="{Binding Path=Customerdetails.Name}" />
</StackPanel>
<ItemsPresenter Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
Upvotes: 0
Views: 271
Reputation: 454
This two solutions work for me thank to @mm8 :
<local:NameConverter x:Key="NameConverter"/>
<TextBox Text="{Binding Converter={StaticResource NameConverter}, Mode=OneWay}"
public class NameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((CustomerDate)((CollectionViewGroup)value).Items[0]).Customerdetails.Name;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Items[0] is an Object of Class CustomerDate so its working this way too:
<TextBlock Text="{Binding Items[0].Customerdetails.Name}" />
Upvotes: 0
Reputation: 169160
The DataContext
of a GroupItem
is a CollectionViewGroup
and not Customerdetails
.
A group may contain several Customerdetails
. You can bind to the Name
property of any of them using the Items
property of the CollectionViewGroup
, e.g.:
<TextBlock Text="{Binding Items[0].Name}" />
Upvotes: 1