我知道以前有人问过这个问题。我正在检查是否有任何新的聪明的解决方案。有没有更好的方法来绑定(bind) SelectedIndex
的 DataGridComboBoxColumn
?这就是我在谷歌搜索后获得绑定(bind)工作的方式:
<!-- 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>
最佳答案
这是绑定(bind) 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>
关于c# - 绑定(bind) DataGridComboBoxColumn SelectedIndex,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14466026/