绑定TextBlock的标记

时间:2013-12-18 15:59:42

标签: c# wpf datagrid

我有一个DataGrid,如下图所示:

enter image description here

在第三级DataGrid中,Date列是DataGridTemplateColumn。

以下是上述日期栏的代码:

<DataGridTemplateColumn Header="Date" Width="5*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding StringFormat='{}{0:dd/MM/yyyy}'}"
                       Tag= ?????? />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

如上面的代码所述,我想将TextBlock的Tag属性绑定到此列中相同日期的出现次数。

我想要的例子:

假设我有5行

   Date      |    Tag
-------------+----------
20/08/2012   |     1
20/08/2012   |     2
21/08/2012   |     1
23/08/2012   |     1
20/08/2012   |     3

假设您没有代码,请告诉我我将尝试自己编写代码的逻辑。

更新:上图中显示的数据网格的完整代码

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding DataContext.Patients, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
          SelectedItem="{Binding SelectedPatient}" RowDetailsVisibilityMode="VisibleWhenSelected"
          IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow" >
    <DataGrid.Resources>
        <Style x:Key="VerticalCenter" TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
        <Style x:Key="VerticalAndHorizontalCenter" TargetType="FrameworkElement" >
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        </Style>
        <Style x:Key="VerticalAndHorizontalCenterTextBlock" TargetType="TextBlock"
               BasedOn="{StaticResource VerticalAndHorizontalCenter}"/>
        <Style x:Key="VerticalAndHorizontalCenterHeader" TargetType="{x:Type DataGridColumnHeader}" 
               BasedOn="{StaticResource VerticalAndHorizontalCenter}"/>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding PatientName}" Header="Patient Name" Width="25*" 
                            ElementStyle="{StaticResource VerticalCenter}"/>
        <DataGridTextColumn Binding="{Binding City}" Header="City" Width="15*" 
                            ElementStyle="{StaticResource VerticalAndHorizontalCenterTextBlock}" 
                            HeaderStyle="{StaticResource VerticalAndHorizontalCenterHeader}"/>
        <DataGridTextColumn Binding="{Binding TypeSex.Value}" Header="Sex" Width="10*" 
                            ElementStyle="{StaticResource VerticalAndHorizontalCenterTextBlock}"
                            HeaderStyle="{StaticResource VerticalAndHorizontalCenterHeader}"/>
        <DataGridTextColumn Binding="{Binding Age}" Header="Age" Width="5*" 
                            ElementStyle="{StaticResource VerticalAndHorizontalCenterTextBlock}" 
                            HeaderStyle="{StaticResource VerticalAndHorizontalCenterHeader}"/>
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Focusable="False" Command="{Binding DataContext.DeletePatientCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                        <Image Source="Images/DeletePatient.png" />
                    </Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid AutoGenerateColumns="False" IsReadOnly="True" 
                      ItemsSource="{Binding DataContext.ReportNames, 
                                            RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
                      SelectedItem="{Binding DataContext.SelectedReportGroup, 
                                             RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
                      Margin="200,0,0,0" Width="500" HorizontalAlignment="Left">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding}" Header="Report Name" Width="5*" 
                                        ElementStyle="{StaticResource VerticalCenter}"/>
                    <DataGridTemplateColumn Header="Delete" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Focusable="False" Command="{Binding DataContext.DeleteAllReportsOfATypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                                    <Image Source="Images/DeleteReport.png" Height="48" Width="48" />
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>

                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <DataGrid AutoGenerateColumns="False" IsReadOnly="True"
                                  ItemsSource="{Binding DataContext.CorrespondingDates, 
                                                      RelativeSource={RelativeSource FindAncestor, 
                                                                      AncestorType=Window}}"
                                  SelectedItem="{Binding DataContext.SelectedReport,
                                                        RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                  Width="390" HorizontalAlignment="Right">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Date" Width="5*"
                                                    HeaderStyle="{StaticResource VerticalAndHorizontalCenterHeader}">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding StringFormat='{}{0:dd/MM/yyyy}'}"
                                                       Tag="{Binding DataContext.Tag,
                                                                     RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                                       Style="{StaticResource VerticalAndHorizontalCenterTextBlock}"/>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTemplateColumn Header="Edit" Width="1.5*">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Focusable="False" Click="OpenTabForEdit" 
                                                    Command="{Binding DataContext.EditSingleReportCommand,
                                                                      RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                                                <Image Source="Images/EditReport.png" Height="48" Width="48" />
                                            </Button>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTemplateColumn Header="Delete" Width="1.5*">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Focusable="False" 
                                                    Command="{Binding DataContext.DeleteSingleReportCommand, 
                                                                      RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                                                <Image Source="Images/DeleteReport.png" Height="48" Width="48"/>
                                            </Button>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>
                        </DataGrid>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>

            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

更新2:类结构

enter image description here

类似于BloodChemistryUrineAnalysis

1 个答案:

答案 0 :(得分:2)

在我看来,你需要MultiBinding,因为你有两个输入:整个集合,以及你想要计算的具体日期。 XAML语法看起来像这样:

<TextBlock>
    <TextBlock.Resources>
        <my:ItemCountMultiConverter x:Key="ItemCountMultiConverter" />
    </TextBlock.Resources>
    <TextBlock.Tag>
        <MultiBinding Converter="{StaticResource ItemCountMultiConverter}">
            <Binding Path="." />
            <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=DataGrid}"  />
        </MultiBinding>
    </TextBlock.Tag>
</TextBlock>

然后,只需要实现ItemCountMultiConverter : IMultiValueConverter从集合中提取日期计数:

public class ItemCountMultiConverter : IMultiValueConverter 
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null || values.Length < 2)
            return DependencyProperty.UnsetValue;

        var dateToCount = values[0] as DateTime?;
        var source = values[1] as IEnumerable<Patient>;

        if (!dateToCount.HasValue || source == null)
            return DependencyProperty.UnsetValue;

        source.Count(patient => 
            patient.Haemogram.Any(haemogram => haemogram.Date == dateToCount)
            || patient.UrineAnalysis.Any(urine => urine.Date == dateToCount)
            || patient.BloodChemistry.Any(blood => blood.Date == dateToCount)
        );
    }

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