仔细查看 ComboBox 及其相关属性的功能可能会有所帮助。您遇到的问题可能与“.Text”字段未反映当前选定的项目有关。
SelectedItem:获取或设置 ComboBox 中当前选定的项目。
基于 ComboBox.SelectionChangeCommitted
文本: 获取或设置与此控件关联的文本。 (覆盖 Control.Text。)
设置文本值将更改组合框的当前值
SelectedValue: 获取或设置由 ValueMember 属性指定的成员属性的值。 (继承自 ListControl。)
基于 ListControl.SelectedValueChanged
来源msdn
进一步阅读dotnetperls。
构建我制作的这个演示程序,以便学习查看它的实际效果。
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="220" Width="711" Background="#FF6937D4">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
<ComboBox x:Name="comboBox1" SelectedValuePath="Content" HorizontalAlignment="Left" Margin="10,7,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboSelectChanged">
<ComboBoxItem Content="Zero" Tag="Tag_Zero"/>
<ComboBoxItem Content="One" Tag="Tag_One"/>
<ComboBoxItem Content="Two" Tag="Tag_Two"/>
<ComboBoxItem Content="Three" Tag="Tag_Three"/>
</ComboBox>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="10,66,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
<TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="10,94,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
<TextBox x:Name="textBox4" HorizontalAlignment="Left" Height="23" Margin="10,122,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
<Label Content="comboBox1.SelectedItem.ToString()" HorizontalAlignment="Left" Margin="348,38,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
<Label Content="comboBox1.Text" HorizontalAlignment="Left" Margin="348,66,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
<Label Content="comboBox1.SelectedIndex.ToString()" HorizontalAlignment="Left" Margin="348,94,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
<Label Content="comboBox1.SelectedValue.ToString()" HorizontalAlignment="Left" Margin="348,122,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
<TextBox x:Name="textBox5" HorizontalAlignment="Left" Height="23" Margin="10,150,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
<Label Content=" (comboBox1.SelectedItem as ComboBoxItem).Content as string" HorizontalAlignment="Left" Margin="348,150,-119,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="360" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
<Button x:Name="btnSelect" Content="Select based on value" HorizontalAlignment="Left" Margin="175,7,0,0" VerticalAlignment="Top" Width="168" Click="btnSelect_Click"/>
</Grid>
XAML.CS
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
private void comboSelectChanged(object sender, SelectionChangedEventArgs e) {
textBox1.Text = comboBox1.SelectedItem.ToString();
textBox2.Text = comboBox1.Text;
textBox3.Text = comboBox1.SelectedIndex.ToString();
textBox4.Text = comboBox1.SelectedValue.ToString();
textBox5.Text = (comboBox1.SelectedItem as ComboBoxItem).Content as string;
}
private void btnSelect_Click(object sender, RoutedEventArgs e) {
// Winform working code: comboBox1.SelectedIndex = comboBox1.FindString("string");
// WPF - This REQUIRES "SelectedValuePath="Content"" in XAML combobox def.
comboBox1.SelectedValue = "Three";
}
}
}