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

C# 使用需要参数的转换器的可重用数据模板(在参考资料中),c#,wpf,xaml,telerik,datatemplate,C#,Wpf,Xaml,Telerik,Datatemplate,我必须在网格中显示12列(每个月一列)不同的数据。 为了正确显示数据,我对每一列和多绑定使用DataTemplates。多重绑定指定一个转换器,该转换器使用月份索引参数化 <DataTemplate x:Key="CustomerForecastQuantityDataTemplate"> <TextBlock TextAlignment="Right"> <TextBlock.Text> <MultiB

我必须在网格中显示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看起来更优雅,将一个DataTemplate定义为静态资源,并在任何地方使用它,如下所示:

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

我的问题是,如何使此数据模板根据每个ForecastCustomerMonthQuantityGridViewDataColumn的MonthIndex值采用不同的参数


非常感谢,我们非常感谢您的每一个建议(可能是我对DataTemplate的概念没有很好的理解)

我想不出一个XAML唯一的方法来从单元格模板中获取“MonthIndex”(顺便提一句,
ConverterParameter
不支持绑定)。在我的脑海中,你可以尝试这样的东西(我不在电脑旁,所以自己无法尝试):-

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

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


您的多值转换器现在可以访问该单元格,而该单元格又会公开一个
属性,您可以将该属性转换为
ForecastCustomerMonthQuantityGridViewDataColumn
,以获取“MonthIndex”。

这是一个非常好的建议,它可以工作!谢谢。回想起来,我也意识到我把事情复杂化了。