绑定数据网格组合框列选定索引

本文关键字:列选 索引 组合 数据 数据网 网格 绑定 | 更新日期: 2023-09-27 17:55:34

我知道以前有人问过这个问题。我正在检查是否有任何新的聪明解决方案。有没有更好的方法来绑定DataGridComboBoxColumnSelectedIndex?这就是我在谷歌搜索后让绑定工作的方式:

<!-- This is a little ridiculous -->
<DataGridComboBoxColumn Header="Batch Type"
    ItemsSource="{Binding Source={StaticResource methodOfPaymentItemsProvider}}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="SelectedIndex" 
                    Value="{Binding MethodOfPayment, 
                                    UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="SelectedIndex" 
                    Value="{Binding MethodOfPayment, 
                                    UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

绑定数据网格组合框列选定索引

这是绑定 DataGridComboBoxColumn 的 SelectedIndex、ItemSource、IsReadOnly 的最佳答案。

<DataGridComboBoxColumn Header="Batch Type"
  ItemsSource="{Binding Source={StaticResource methodOfPaymentItemsProvider}}">
  <DataGridComboBoxColumn.ElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="SelectedIndex" Value="{Binding MethodOfPayment,  UpdateSourceTrigger=PropertyChanged}" />
        <Setter Property="ItemsSource" Value="{Binding Streets, RelativeSource= {RelativeSource FindAncestor,AncestorType=UserControl}, Mode=OneWay}"/>
        <Setter Property="IsReadOnly" Value="True"/>
    </Style>
 </DataGridComboBoxColumn.ElementStyle>
 <DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="SelectedIndex" Value="{Binding MethodOfPayment, UpdateSourceTrigger=PropertyChanged}" />
        <Setter Property="ItemsSource" Value="{Binding Streets, RelativeSource={RelativeSource FindAncestor,AncestorType=UserControl}, Mode=OneWay}"/>
     </Style>
 </DataGridComboBoxColumn.EditingElementStyle>
 </DataGridComboBoxColumn>