2016-12-09 14 views
4

これをどのように達成するかはわかりませんが、ListBoxには日付と時刻の列があります。日付が既にそこにあった場合、日付列は表示されません。私はListCollectionViewとListview/DataGridsとの組み合わせで、それが可能であることを知っています。しかし、これをListBoxとListで実現できますか?私はMVVMの原則を使用していることを覚えておいてください。これは私のリストボックスです:ListBoxを日付付きでグループ化する

<ListBox Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Schedules}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Background="Transparent"> 
          <Grid Background="Transparent"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="100"/> 
            <ColumnDefinition Width="100"/> 
            <ColumnDefinition/> 
           </Grid.ColumnDefinitions> 
           <Grid.RowDefinitions> 
            <RowDefinition/> 
           </Grid.RowDefinitions> 
           <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding MyDateTime, StringFormat='d' }" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
           <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding MyDateTime, StringFormat='t'}" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
           <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding SomeText}" TextTrimming="WordEllipsis" LineStackingStrategy="MaxHeight" MaxHeight="20" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
          </Grid> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

私は私が達成しようとしているものの例を与えるために、Excelでこれを作っています enter image description here

私は黄色

+1

はあなたのためにも許容できる別のレイアウトですか?あなたはグルーピング機能を使用することができます:http://www.wpf-tutorial.com/listview-control/listview-grouping/ – Sentry

答えて

1

DateTimeText コンバータ

// Order Schedules using System.Linq 
public class ToOrderedListConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     List<ScheduleItem> schedules = (List<ScheduleItem>)value; 
     var subset = from item in schedules 
        orderby item.MyDateTime.TimeOfDay 
        orderby item.MyDateTime.ToString("yyyy/MM/dd") descending 
        select item; 
     return subset.ToList(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

// Show only first occurrence of date 
public class DateToVisibilityConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     DateTime currentItem = (DateTime)values[0]; 
     List<ScheduleItem> schedules = (List<ScheduleItem>)values[1]; 
     ScheduleItem firstOccurrence = 
      schedules.Find(item => item.MyDateTime.Year == currentItem.Year 
           && item.MyDateTime.Month == currentItem.Month 
           && item.MyDateTime.Day == currentItem.Day); 

     if (firstOccurrence.MyDateTime == currentItem) 
      return Visibility.Visible; 
     else return Visibility.Collapsed; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML

<ListBox Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     ItemsSource="{Binding Schedules, Converter={StaticResource ToOrderedListConverter}}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Background="Transparent"> 
       <Grid Background="Transparent"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="100"/> 
         <ColumnDefinition Width="100"/> 
         <ColumnDefinition/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding MyDateTime, StringFormat='dd/MM/yyyy'}" HorizontalAlignment="Left" VerticalAlignment="Center"> 
         <TextBlock.Visibility> 
          <MultiBinding Converter="{StaticResource DateToVisibilityConverter}"> 
           <Binding Path="MyDateTime"/> 
           <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListBox}}" 
             Path="ItemsSource"/> 
          </MultiBinding> 
         </TextBlock.Visibility> 
        </TextBlock> 
        <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding MyDateTime, StringFormat='t'}" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
        <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding SomeText}" TextTrimming="WordEllipsis" LineStackingStrategy="MaxHeight" MaxHeight="20" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
       </Grid> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

ありがとう、私は恐れていましたが、それは不可能ですが、これは完璧です。私は感謝します。 Althouh同じ日付のいくつかは時間(時と分)が同じ場所で2回表示されます。しかし、私は自分自身でそれを理解します。あなたのソリューションは、私が欲しいものを96%完成させ、それが私を正しい方向に導きます。百万人の仲間に感謝します – user1702369

関連する問題