将行为绑定到silverlight中的datatemplate的datacontext

时间:2012-02-09 10:17:02

标签: data-binding silverlight-4.0 datatemplate behavior

我有一个DataTemplate:

  <Style x:Key="SalesmanDestinyReportListBoxStyle" TargetType="ListBox">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ItemsPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <telerik:RadGridView x:Name="grdSalesmanDestinyReport" Width="180" SelectionMode="Single" IsReadOnly="False" IsFilteringAllowed="True"
                     VerticalAlignment="Stretch" RowIndicatorVisibility="Collapsed" HorizontalAlignment="Stretch"
                     CanUserDeleteRows="False" CanUserInsertRows="False" CanUserReorderColumns="False" ItemsSource="{Binding Scores}" AutoGenerateColumns="False">
                           <i:Interaction.Behaviors>
                                <Binding:ColumnGroupsBehavior>
                                    <Binding:ColumnGroupsBehavior.CommonHeaders>
                                        <Binding:CommonHeader StartColumnIndex="0" Caption="{Binding Path=DataContext.SalesmanName,ElementName=grdSalesmanDestinyReport}" ColumnSpan="3" />
                                    </Binding:ColumnGroupsBehavior.CommonHeaders>
                                </Binding:ColumnGroupsBehavior>
                            </i:Interaction.Behaviors>
                            <telerik:RadGridView.Columns>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding Current}" IsReadOnly="True"
                             Header="{Binding Current}" Width="60">
                                    <telerik:GridViewDataColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Current}">

                                            </TextBlock>
                                        </DataTemplate>
                                    </telerik:GridViewDataColumn.CellTemplate>
                                </telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding Destiny}" IsReadOnly="True"
                             Header="{Binding Current}" Width="60">
                                    <telerik:GridViewDataColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Destiny}">

                                            </TextBlock>
                                        </DataTemplate>
                                    </telerik:GridViewDataColumn.CellTemplate>
                                </telerik:GridViewDataColumn>

                                <telerik:GridViewDataColumn DataMemberBinding="{Binding Best}" IsReadOnly="True"
                             Header="{Binding Current}" Width="60">
                                    <telerik:GridViewDataColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Best}">

                                            </TextBlock>
                                        </DataTemplate>
                                    </telerik:GridViewDataColumn.CellTemplate>
                                </telerik:GridViewDataColumn>

                            </telerik:RadGridView.Columns>
                        </telerik:RadGridView>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这是一个源类:

public class SalesmanDestinyReport
    {
        [DataMember]
        public List<Score> Scores { get; set; }

        [DataMember]
        public string SalesmanName { get; set; }
    }

我对这段代码有疑问:

                       <i:Interaction.Behaviors>
                            <Binding:ColumnGroupsBehavior>
                                <Binding:ColumnGroupsBehavior.CommonHeaders>
                                    <Binding:CommonHeader StartColumnIndex="0" Caption="{Binding Path=DataContext.SalesmanName,ElementName=grdSalesmanDestinyReport}" ColumnSpan="3" />
                                </Binding:ColumnGroupsBehavior.CommonHeaders>
                            </Binding:ColumnGroupsBehavior>
                        </i:Interaction.Behaviors>

问题是我无法将SalesmanName属性传递给

   <Binding:CommonHeader StartColumnIndex="0" Caption="{Binding Path=DataContext.SalesmanName,ElementName=grdSalesmanDestinyReport}" ColumnSpan="3" />

我认为没有必要解释wthat是CommonHeader,因为我认为问题存在于另一个地方,但以防万一:

http://blogs.telerik.com/pavelpavlov/posts/09-12-04/adding-additional-power-to-radgridview-for-silverlight-with-attached-behaviors.aspx

我需要在绑定声明中更改以显示SalesmanName吗?

1 个答案:

答案 0 :(得分:1)

从头开始 - Silverlight 4不支持样式设置器中的绑定(此功能现在可在SL5中使用)。对于SL4,有一种解决方法:SetterValueBindingHelper