【问题标题】:Can't change ComboBox selected item无法更改 ComboBox 选定项
【发布时间】:2013-07-15 22:34:12
【问题描述】:

我一整天都在努力让它正常工作,但似乎我做不到。

我做的第一件事是用系统上安装的声音列表填充一个组合框。一旦组合框正确填充了所有声音,我就无法从组合框中选择任何值。

问题出在哪里?

public class ComboData
{
public int _Id { get; set; }
public string _Name { get; set; }
}

public void accSettings()
{
SpeechSynthesizer speaker = new SpeechSynthesizer();

List<ComboData> ListData = new List<ComboData>();

int a = 1;
foreach (InstalledVoice voice in speaker.GetInstalledVoices())
{
    VoiceInfo info = voice.VoiceInfo;
    ListData.Add(new ComboData { _Id = a, _Name = info.Name });
    a++;
}

VoiceList.ItemsSource = ListData;
VoiceList.DisplayMemberPath = "_Name";
VoiceList.SelectedValuePath = "_Id";

VoiceList.SelectedValue = "1";
}

这是 XAML

<ComboBox x:Name="VoiceList" Margin="10,0,0,0" Width="200" HorizontalAlignment="Left"/>

与我的问题相关的所有 XAML:

<Window x:Class="UltimateParkinson.UltimateView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CIRENC - Base de Datos" Height="582" Width="830" WindowStartupLocation="CenterScreen" Closed="Window_Closed">
<ContentControl x:Name="content_ajustes" Margin="85,67,15,50" UseLayoutRounding="False" Visibility="Visible">
    <Grid Margin="0" Background="#FFF9F9F9">
        <TabControl x:Name="PreferencesTab" Margin="0" SelectionChanged="PreferencesTab_SelectionChanged">
            <TabItem Header="Accesibilidad">
                <Grid Background="#FFE5E5E5">
                    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Desde aquí puedes establecer los ajustes de accesibilidad de la aplicación." VerticalAlignment="Top" FontWeight="Bold"/>
                    <GroupBox Header="Lector de Textos" Margin="10,35,10,0" VerticalAlignment="Top">
                        <Grid Margin="0">
                            <StackPanel Margin="0" Orientation="Vertical">
                                <TextBlock Margin="5" Text="¿Activar el lector de textos?" FontWeight="Bold"></TextBlock>
                                <RadioButton x:Name="readerYes" Margin="10,0,10,0" Content="Si"/>
                                <RadioButton x:Name="readerNo" Margin="10,0,10,0" Content="No"/>
                                <TextBlock Margin="5" Text="¿Qué voz deséa utilizar?" FontWeight="Bold"></TextBlock>
                                <ComboBox x:Name="VoiceList" Margin="10,0,0,0" Width="200" HorizontalAlignment="Left"/>
                                <TextBlock FontSize="11" Margin="15,0,10,0" Text="* Las voces están instaladas en tú sistema y son ajenas a la aplicación."></TextBlock>
                                <TextBlock Margin="5" Text="Velocidad de lectura" FontWeight="Bold"></TextBlock>
                                <ComboBox x:Name="VoicePitch" Margin="10,0,0,15" Width="50" HorizontalAlignment="Left">
                                    <ComboBoxItem Content="-2"/>
                                    <ComboBoxItem Content="-1"/>
                                    <ComboBoxItem Content="0"/>
                                    <ComboBoxItem Content="1"/>
                                    <ComboBoxItem Content="2"/>
                                </ComboBox>
                                <TextBlock Margin="5,0,10,0" Text="*Puedes probar la voz aquí." FontWeight="Bold" FontSize="11"></TextBlock>
                                <StackPanel Orientation="Horizontal" Margin="10,0,10,0">
                                    <TextBlock Text="Texto de prueba." Padding="0,5,0,0"></TextBlock>
                                    <Button Content="Vista Previa" HorizontalAlignment="Left" Padding="5" Margin="10,0,0,10" ></Button>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </GroupBox>
                    <Button x:Name="guardarAcc" Padding="5" Content="Guardar" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Click="guardarAcc_Click"/>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</ContentControl>
</Window>

【问题讨论】:

  • 我无法选择任何值。这是因为控件是灰色的吗?您确定 Enabled 没有设置为 false 吗?查看设计器中的属性,看看是否打开了某些恶意选项。
  • 你能显示 VoiceList 的 XAML 吗?
  • @gunr2171 控件已正确启用,我可以显示下拉列表,问题是一旦我单击组合框项目之一,没有任何反应,下拉列表消失,就像您选择一个项目但什么都没有之后选择。
  • (来自 WinForms 背景)您是否有 SelectedIndexChanged 事件处理程序。做出选择后,您的代码如何知道如何执行任何操作?
  • @gunr2171 我没有向组合框添加任何类型的事件处理程序。会不会在别的地方?

标签: c# wpf combobox


【解决方案1】:

accSettings() 是否被多次调用?把断点放在那里看看。如果您单击一个项目,然后再次调用 accSettings(),组合框将重置为其初始值,您的选择将丢失。

【讨论】:

  • 是的,这就是我发现的,我是从 ControlTab 选择更改事件中调用它,我以为它只会在选项卡之间切换时触发,我错了。谢谢
猜你喜欢
  • 2018-07-30
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
相关资源
最近更新 更多