【问题标题】:How to bind List<string> to RadGridView DataColumn如何将 List<string> 绑定到 RadGridView DataColumn
【发布时间】:2012-11-09 13:02:42
【问题描述】:

我正在为 Silverlight 使用 Telerik RadControls,我需要在一列中绘制字符串列表。我已将列的 cellStyle 绑定到一种样式,并在该样式中添加了我的 ListCellPresenter 控件的实例。我的控件使用依赖属性 DataList 接收其内部数据。如何将需要绘制的实际数据绑定到 GridView,以便在 ListCellPresenter 中可以访问?

<Grid x:Name="LayoutRoot">
    <telerik:RadGridView x:Name="gvMain" AutoGenerateColumns="True">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn Width="200" CellStyle="{StaticResource listStyle}" Header="New Column">
            </telerik:GridViewDataColumn>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

风格如下:

<UserControl.Resources>
    <Style TargetType="grid:GridViewCell" x:Key="listStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="grid:GridViewCell">
                    <local:ListCellPresenter DataList="{Binding DataList}"></local:ListCellPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

【问题讨论】:

    标签: silverlight xaml data-binding gridview telerik


    【解决方案1】:

    应该很简单——只需使用DataMemberBinding 指向包含要绑定到的列表的行对象的属性。即:

    <gridView:GridViewDataColumn 
        CellStyle="{StaticResource listStyle}" Header="New Column"  
        DataMemberBinding="{Binding TheListProperty}" /> 
    

    (这里假设RadGridViewItemsSource 类似于IEnumerable&lt;MyModel&gt;,其中MyModel 包含您的目标List&lt;string&gt; TheListProperty。)

    【讨论】:

    • 谢谢。这解决了我的问题并节省了我很多时间。
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 2021-06-28
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多