WPF Combobox问题

时间:2013-08-05 12:25:37

标签: c# wpf combobox

对你们所有人来说只是一个简单的问题。

我有两个组合框,其中一个组合数据硬编码:

<ComboBox>
    <ComboBoxItem/>
</ComboBox>

当我尝试在SelectionChanged上使用case语句时,我从Convert.ToString(cboForm.SelectedValue): "System.Windows.Controls.ComboBoxItem: Directional Bore"得到一个值。

现在在同一个表单上,我有另一个组合框,其中包含我填充comboboc.items.add()的数据,而SelectionChanged之后的组合框的值是实际文本值。

我能做些什么才能在所选值中没有"System.Windows.Controls.ComboBoxItem: "

案例陈述:

string caseSwitch = cboForm.SelectedValue.ToString();
switch (caseSwitch)
{
    case "System.Windows.Controls.ComboBoxItem: Directional Bore":
    {
        ReportClass.DirBoreReport("","","");
        cboForm.SelectedIndex = -1;
        //MainAccess.Mw.SpecificForm = false;
        //MainAccess.Mw.frmPage.Height = 4400;
        //MainAccess.Mw.HoldPage = new Uri("DirectionalBore_Combined.xaml", UriKind.Relative);
        //if (NavigationService != null) NavigationService.Source = MainAccess.Mw.HoldPage;
        //MainAccess.Mw.SectionBar_DirectionalBore.Visibility = Visibility.Visible;
        //MainAccess.Mw.scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        //MainAccess.Mw.lblForm.Content = "DIRECTIONAL BORE";
        //MainAccess.Mw.Sbar = MainAccess.Mw.SectionBar_DirectionalBore;
        break;
    }
}

的Xaml:

<ComboBox x:Name="cboForm" TabIndex="3" HorizontalAlignment="Left" Margin="746,618,0,0" VerticalAlignment="Top" Width="630" FontSize="48" FontWeight="Bold" Height="74" SelectionChanged="cboForm_SelectionChanged" >
    <ComboBoxItem Content="Directional Bore" HorizontalAlignment="Left"  />
    <ComboBoxItem Content="Drain Tile Break" HorizontalAlignment="Left"/>
    <ComboBoxItem Content="Fiber Installation" HorizontalAlignment="Left"/>
</ComboBox>

谢谢!

1 个答案:

答案 0 :(得分:5)

首先将SelectedItem转换为ComboBoxItem,然后ComboBoxItem.Content将包含字符串值。

        ComboBoxItem item = this.cboForm.SelectedItem as ComboBoxItem;
        if (item != null)
        {
            MessageBox.Show(item.Content.ToString());
        }