在 silverlight 中将行为绑定到数据模板的数据上下文
我有一个 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" />
我认为没有必要解释 CommonHeader 是什么,因为问题我相信位于另一个地方,但以防万一:
我需要在绑定声明中更改哪些内容才能显示 SalesmanName ?
I have a 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>
here is a source class:
public class SalesmanDestinyReport
{
[DataMember]
public List<Score> Scores { get; set; }
[DataMember]
public string SalesmanName { get; set; }
}
and I have a problem with this piece of code:
<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>
The problem is that I can't pass SalesmanName property to
<Binding:CommonHeader StartColumnIndex="0" Caption="{Binding Path=DataContext.SalesmanName,ElementName=grdSalesmanDestinyReport}" ColumnSpan="3" />
I think that there is no need to explain wthat's the CommonHeader, because the problem I believe lays in another place, but just in case:
What I need to change in my binding declaration to display SalesmanName ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从头开始 - Silverlight 4 不支持样式设置器中的绑定(此功能现已在 SL5 中提供)。对于 SL4,有一个解决方法: SetterValueBindingHelper。
From scratch - Silverlight 4 does not support bindings in Style Setters (this functionality is now available in SL5). For SL4 there is a workaround: SetterValueBindingHelper.