如何在DevExpress ComboBoxEdit中设置索引?

时间:2011-11-04 15:48:15

标签: c# wpf xaml devexpress

如何在DevExpress SelectedIndex中“设置”ComboBoxEdit
我在 XAML 和后面的代码中都试过了,但索引没有设置,它以空白项开始。

我的XAML: [我看不出为什么这不起作用,但它没有..]

<dxb:BarEditItem.EditSettings>
    <dxe:ComboBoxEditSettings>
        <dxe:ComboBoxEditSettings.Items>
            <dxe:ComboBoxEditItem IsSelected="True">AAA</dxe:ComboBoxEditItem>
            <dxe:ComboBoxEditItem>BBB</dxe:ComboBoxEditItem>
            <dxe:ComboBoxEditItem>CCC</dxe:ComboBoxEditItem>
        </dxe:ComboBoxEditSettings.Items>
    </dxe:ComboBoxEditSettings>
</dxb:BarEditItem.EditSettings>

我的C#代码:
[我正确地获取 countStr 所以我确定ComboBoxEdit并且项目已初始化,添加好了,但是SelectedIndex仍然没有设置索引..]
*我也不想使用EditValue来设置值,我需要使用整数(索引)来设置它。

private void Foo_LinkControlLoaded(object sender,
    DevExpress.Xpf.Bars.BarItemLinkControlLoadedEventArgs e)
{
    BarEditItemLink link = (BarEditItemLink)sender;
    countStr = ((ComboBoxEdit)link.Editor).Items.Count.ToString();
    ((ComboBoxEdit)link.Editor).SelectedIndex = 2;
}

1 个答案:

答案 0 :(得分:2)

编辑器设置中没有SelectedIndex或SelectedItem属性(例如ComboBoxEditSettings)。 但您可以通过编辑器样式设置SelectedIndexComboBoxEdit,SelectedItem或EditValue属性:

<dxb:BarEditItem x:Name="beiComboBox">
    <dxb:BarEditItem.EditStyle>
        <Style TargetType="dxe:ComboBoxEdit">
            <Setter Property="SelectedIndex" Value="1"/>
        </Style>
    </dxb:BarEditItem.EditStyle>
    <dxb:BarEditItem.EditSettings>
        <dxe:ComboBoxEditSettings>
            <dxe:ComboBoxEditSettings.Items>
                <dxe:ComboBoxEditItem>AAA</dxe:ComboBoxEditItem>
                <dxe:ComboBoxEditItem>BBB</dxe:ComboBoxEditItem>
                <dxe:ComboBoxEditItem>CCC</dxe:ComboBoxEditItem>
            </dxe:ComboBoxEditSettings.Items>
        </dxe:ComboBoxEditSettings>
    </dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>

如果捕获Loaded事件,您还可以从codebehind设置ComboBoxEdit.SelectedIndex属性:

<dxb:BarEditItem.EditStyle>
    <Style TargetType="dxe:ComboBoxEdit">
        <EventSetter Event="Loaded" Handler="ComboBoxEdit_Loaded"/>
    </Style>
</dxb:BarEditItem.EditStyle>

//...
void ComboBoxEdit_Loaded(object sender, RoutedEventArgs e) {
    ((ComboBoxEdit)sender).SelectedIndex = 1;
}