I need to set a TextBlock’s Text to something like “Name (number of items with not null property)”. Right now, I’m doing this using the number of items of the entire collection, using ItemsSource.Count.
<TextBlock x:Name="textBlockHeader" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource headerCreator}" x:Name="multiBinder">
<Binding ElementName="trackingTable" Path="Name" />
<Binding ElementName="trackingsGrid" Path="ItemsSource.Count" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
For that I’m using an IMultiValueConverter:
internal class HeaderCreator : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Based on this xaml
////<Binding ElementName="trackingTable" Path="Name" /> values[0]
////<Binding ElementName="trackingsGrid" Path="ItemsSource.Count" /> values[1]
return values[0] + " (" + values[1] + ")";
}
trackingsGrid is a DataGrid defined below (not showing code here), binded to the collection, which has TrackingData objects. TrackingData has a property called Tracking. I need to count only the items in the ObservableCollection that has this property as not null. How can I achieve this?
public class TrackingData : INotifyPropertyChanged
{
public Model.ITracking Tracking { get; set; }
...
}
Thanks in advance.
Put this logic (… items in the ObservableCollection that has this property as not null) in your ViewModel and bind to this property.