为 RadGridViewComboBox 列创建 DataTemplate

Create a DataTemplate for a RadGridViewComboBox Column

我想覆盖它并使用 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 中获取状态。

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

数据模板

                <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 位于父级的数据上下文中时使用这种方式。
  3. 来自 xaml ItemsSource="{Binding Source={StaticResource SourceCollection}}"。当您的 SourceCollection 是在 Xaml 中生成的静态集合时使用这种方式(例如;基于枚举类型)。您需要在 <SomeParentVisualAccessibleByridViewDataColumn.Resource> 部分中进行下一个声明。

第三个来源声明 (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 出价不正确,请检查您的输出中是否有相关的绑定错误异常 window。如果您需要任何帮助,请告诉我。

此致。