【问题标题】:Create a DataTemplate for a RadGridViewComboBox Column为 RadGridViewComboBox 列创建 DataTemplate
【发布时间】:2016-05-28 00:24:42
【问题描述】:

我想覆盖它并使用 wpf 原始组合框,而不是使用默认的 telerik gridviewcombobox 模板。在尝试应用数据模板之前它工作得很好。

<Telerik:GridViewComboBoxColumn 
    Header="Status" 
    DataMemberBinding="{Binding Status_Id}" 
    ItemsSource="{Binding Statuses, Mode=TwoWay}" 
    DisplayMemberPath="StatusName"  
    SelectedValueMemberPath="Id">
</Telerik:GridViewComboBoxColumn>

当我尝试应用数据模板时,组合框现在显示空白值。

 <Telerik:GridViewComboBoxColumn Header="Status"
    <Telerik:GridViewComboBoxColumn.CellTemplate>
        <DataTemplate>
            <ComboBox SelectedValue="{Binding Status_Id}" 
                      ItemsSource="{Binding Statuses, Mode=TwoWay}" 
                      DisplayMemberPath="StatusName" 
                      SelectedValuePath="Id">
            </ComboBox>
        </DataTemplate>
    </Telerik:GridViewComboBoxColumn.CellTemplate>
</Telerik:GridViewComboBoxColumn>

我设置的选定值属性值不正确吗?任何帮助将不胜感激。我想当我设置数据模板时,它打错了层。我认为它不再从 Viewmodel 中获取状态。

【问题讨论】:

    标签: c# wpf telerik-grid


    【解决方案1】:

    这是我在项目中使用的模板:

    数据模板

                    <telerik:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=StackOptimizerSelectedRule}"
                                            Header="Rules"
                                            IsFilterable="False" IsReorderable="False">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                            <TextBlock Text="{Binding StackOptimizerSelectedRule, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumTypeConverterKey}}"></TextBlock>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                    
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                            <ComboBox 
                                ItemsSource="{Binding Source={StaticResource StackOptimizerSelectionRules}}"
                                SelectedItem="{Binding StackOptimizerSelectedRule, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                                        <TextBlock Text="{Binding Converter={StaticResource EnumTypeConverterKey}, UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>
    

    说明

    这里有两个模板。当包含单元格不在焦点中时,GridViewDataColumn.CellTemplate 将可用。当包含单元格处于焦点并且用户更改其选择时,CellEditTemplate 将可用。

    请记住接下来的事情,你有几种方法来绑定组合的 ItemsSource:

    1. 常规绑定ItemsSource="{Binding SourceCollection, UpdateSourceTrigger=PropertyChanged}"。当您的 SourceCollection 出现在 Cell DataContext 中时,请使用这种方式。
    2. 相对绑定ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type PutHereTheTypeOfActualParentThatHoldsDataContextYouNeed}}, Path=DataContext.SourceCollection}"。当您的 SourceCollection 在 Parent 的数据上下文中时使用这种方式。
    3. 来自 xaml ItemsSource="{Binding Source={StaticResource SourceCollection}}"。当您的 SourceCollection 是在 Xaml 中生成的静态集合时使用这种方式(例如;基于枚举类型)。您需要 &lt;SomeParentVisualAccessibleByridViewDataColumn.Resource&gt; 部分中的下一个声明。

    第三个来源声明 (in addition read the next article)

    <ObjectDataProvider x:Key="SourceCollection"
                                MethodName="GetValues"
                                ObjectType="{x:Type flowConfiguration:StackOptimizerSelectionRules}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="flowConfiguration:StackOptimizerSelectionRules"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    

    在我看来,您的问题是组合的 ItemsSource 出价不正确,请检查您的输出窗口中是否有相关的绑定错误异常。如果您需要任何帮助,请告诉我。

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多