代码之家  ›  专栏  ›  技术社区  ›  daemont

使用需要参数的转换器的可重用数据模板(在参考资料中)

  •  0
  • daemont  · 技术社区  · 7 年前

    我必须在网格中显示12列(每个月一列)不同的数据。 为了正确显示数据,我对每一列和多绑定使用DataTemplates。多重绑定指定了一个用月份索引参数化的转换器。

     <DataTemplate x:Key="CustomerForecastQuantityDataTemplate">
        <TextBlock TextAlignment="Right">
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource ForecastCustomerQuantityConverter}" **ConverterParameter="0"**>
                                <Binding RelativeSource="{RelativeSource AncestorType={x:Type view:BaseView}}" Path="DataContext.ForecastPeriods"/>
                                <Binding Path="ForecastQuantities"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
    

    我试图使xaml看起来更优雅,将一个数据模板定义为静态资源,并在任何地方使用它,如下所示:

    <forecast:ForecastCustomerMonthQuantityGridViewDataColumn
        Header="{Binding RelativeSource={RelativeSource Self}, Path=MonthIndex}"
        HeaderTextAlignment="Center"
        HeaderTextWrapping="Wrap"
        Width="60"
        IsReadOnly="True"
        MonthIndex="1"
        CellTemplate="{StaticResource CustomerForecastQuantityDataTemplate}"/>
    

    非常感谢,非常感谢您的每一个建议(可能是因为我对数据模板的概念没有很好的理解)

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andrew Stephens    7 年前

    ConverterParamter 不支持绑定)。在我的脑海中,你可以尝试这样的东西(我不在电脑旁,所以我自己无法尝试):-

    向多值转换器添加第三个绑定,绑定到正在模板化的单元格,例如:

    <binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridCell}}" />
    

    您的多值转换器现在可以访问该单元,该单元反过来会暴露一个 Column ForecastCustomerMonthQuantityGridViewDataColumn