Using a Grid as an ItemsPanel
-
I don't think I explained it well enough, what you've got works fine so to speak, if you set the Grid.Row and Grid.Column of the TextBlocks (in this instance) explicitly then it does arrange them in the appropriate "cell". However if you try and set them through binding then it doesn't work. E.g.
<TextBlock Grid.Row="{Binding Path=Start,Converter={StaticResource DateToRowConverter}}"
Grid.Col="{Binding Path=Start,Converter={StaticResource DateToColConverter}}" Text="{Binding Path=Summary}" />Where DateToRowConverter returns say the hour of the event and the DateToColConverter returns the day of week. (Sorry hit the wrong key so you won't get the full post).
Sorry - I get it now (to quote American teens, "My bad"). OK - this is a more complicated issue and one that I can't see off the top of my head how you'd accomplish this. Potentially you could do this by setting the Grid.Row and Grid.Column in the converters.
Deja View - the feeling that you've seen this post before.
-
Sorry - I get it now (to quote American teens, "My bad"). OK - this is a more complicated issue and one that I can't see off the top of my head how you'd accomplish this. Potentially you could do this by setting the Grid.Row and Grid.Column in the converters.
Deja View - the feeling that you've seen this post before.
-
I don't think I explained it well enough, what you've got works fine so to speak, if you set the Grid.Row and Grid.Column of the TextBlocks (in this instance) explicitly then it does arrange them in the appropriate "cell". However if you try and set them through binding then it doesn't work. E.g.
<TextBlock Grid.Row="{Binding Path=Start,Converter={StaticResource DateToRowConverter}}"
Grid.Col="{Binding Path=Start,Converter={StaticResource DateToColConverter}}" Text="{Binding Path=Summary}" />Where DateToRowConverter returns say the hour of the event and the DateToColConverter returns the day of week. (Sorry hit the wrong key so you won't get the full post).
The problem that you have is that you are returning the wrong type of value here. I've just been playing around with a sample of this, and here's a converter sample that you might want to take a look at:
[ValueConversion(typeof(DateTime), typeof(int))]
public class ColDateFormatter : IValueConverter
{
#region IValueConverter Memberspublic object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DateTime startDate = DateTime.Today; // This is just a test to get a date to compare to. TimeSpan off = startDate.Subtract(DateTime.Parse(value as string)); return off.Days + 1; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion
}
Here's a sample of the XAML that you'll need:
<TextBlock Grid.Row="0" Padding="2"
Grid.Column="{Binding XPath=Start, Converter={StaticResource ColDateFormatter}}"
Text="{Binding Mode=OneWay, XPath=Start, Converter={StaticResource Formatter}, ConverterParameter='dd-MMM-yy'}"/>Obviously, you'll need to have added an appropriate number of RowDefinition and ColumnDefinition items. Hmmm. I'm almost tempted to turn this into an article. :-D
Deja View - the feeling that you've seen this post before.
-
The problem that you have is that you are returning the wrong type of value here. I've just been playing around with a sample of this, and here's a converter sample that you might want to take a look at:
[ValueConversion(typeof(DateTime), typeof(int))]
public class ColDateFormatter : IValueConverter
{
#region IValueConverter Memberspublic object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DateTime startDate = DateTime.Today; // This is just a test to get a date to compare to. TimeSpan off = startDate.Subtract(DateTime.Parse(value as string)); return off.Days + 1; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion
}
Here's a sample of the XAML that you'll need:
<TextBlock Grid.Row="0" Padding="2"
Grid.Column="{Binding XPath=Start, Converter={StaticResource ColDateFormatter}}"
Text="{Binding Mode=OneWay, XPath=Start, Converter={StaticResource Formatter}, ConverterParameter='dd-MMM-yy'}"/>Obviously, you'll need to have added an appropriate number of RowDefinition and ColumnDefinition items. Hmmm. I'm almost tempted to turn this into an article. :-D
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
The problem that you have is that you are returning the wrong type of value here
My converter was constructed as such:
public class DateToRowConverter : IValueConverter
{
public object ConvertTo(object value, Type targetType, object parameter, CultureInfo culture)
{
// No checking for simplicity
return ((DateTime)value).Hour - 8; // Grid starts at 08:00 for example so this provides the necessary offset
}
// Rest of class...
}I didn't have those attributes so perhaps that was what was missing. I'll see if I can get a sample project made up for you to illustrate the point...
-
Pete O'Hanlon wrote:
The problem that you have is that you are returning the wrong type of value here
My converter was constructed as such:
public class DateToRowConverter : IValueConverter
{
public object ConvertTo(object value, Type targetType, object parameter, CultureInfo culture)
{
// No checking for simplicity
return ((DateTime)value).Hour - 8; // Grid starts at 08:00 for example so this provides the necessary offset
}
// Rest of class...
}I didn't have those attributes so perhaps that was what was missing. I'll see if I can get a sample project made up for you to illustrate the point...
And do you have enough RowDefinition and ColumnDefinition items in the XAML? This is important.
Deja View - the feeling that you've seen this post before.
-
And do you have enough RowDefinition and ColumnDefinition items in the XAML? This is important.
Deja View - the feeling that you've seen this post before.
-
And do you have enough RowDefinition and ColumnDefinition items in the XAML? This is important.
Deja View - the feeling that you've seen this post before.
Sorted it out, ;P Thanks to someone on the WPF forum @ MSDN (god I miss the CP forums compared to those). Basically everything I was doing was correct but didn't realise that the TextBlock / ItemTemplate is bound by a ListItem so you have to set the Grid.Row and Grid.Column from there. E.g.
<Style TargetType="{x:Type ListItem}">
<Setter PropertyName="Grid.Row" Value="{Binding Path=Start, Converter={StaticResource DateToRowConverter}}" />
<Setter PropertyName="Grid.Column" Value="{Binding Path=Start, Converter={StaticResource DateToColConverter}}" />
</Style> -
Sorted it out, ;P Thanks to someone on the WPF forum @ MSDN (god I miss the CP forums compared to those). Basically everything I was doing was correct but didn't realise that the TextBlock / ItemTemplate is bound by a ListItem so you have to set the Grid.Row and Grid.Column from there. E.g.
<Style TargetType="{x:Type ListItem}">
<Setter PropertyName="Grid.Row" Value="{Binding Path=Start, Converter={StaticResource DateToRowConverter}}" />
<Setter PropertyName="Grid.Column" Value="{Binding Path=Start, Converter={StaticResource DateToColConverter}}" />
</Style>:doh: I should have realised that. Stoopid stoopid me.
Deja View - the feeling that you've seen this post before.
-
:doh: I should have realised that. Stoopid stoopid me.
Deja View - the feeling that you've seen this post before.
-
-