4

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

<!-- 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>
4

1 回答 1

1

这是绑定 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>
于 2014-09-15T12:24:51.743 回答