Just getting confused when trying to display certain things when utilising itemssource, datacontexts and binding.
public class Date
{
public DateTime _WeekDate;
public ICollectionView _WeekData;
}
public class MainWindowViewModel
{
public ICollectionView WeekDates { get; private set; }
public MainWindowViewModel()
{
List<Date> _dates = new List<Date>();
//Code to populate _dates
WeekDates = CollectionViewSource.GetDefaultView(_dates);
}
}
public MainWindow()
{
InitializeComponent();
_ViewModel = new MainWindowViewModel();
gMain.DataContext = _ViewModel;
}
So as you can see I’ve got a collection of a class called Date, which has a DateTime and another collection inside it. This is all created in a ViewModel which is attached to a grid as a data context.
Within this grid I have a combobox which needs to display the DateTime value listed in each Date class within the collection. So with much messing about I have this:
<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="WeekDate" />
This has worked however I also want a binding converter to be applied to the MemberPath to change the style in which the date will be displayed. But applying it this way I can’t add the Converter. To no avail, I’ve tried things such as:
<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="{Binding Path=WeekDate", Converter={StaticResource DateFormatter}/>
Any ideas? Or any better ways of doing this?
Thanks in advance,
SumGuy
You can’t bind to the DisplayMemberPath but what you can do is provide a property within your bound object, similar to WeekDate in the working example you provided, that returns the WeekDate in the format you wish. When WeekDate is set, raise the propertychanged event for the new formatting property.