无法更改ComboBox所选项目

时间:2013-07-15 14:00:40

标签: c# wpf combobox

我整天都在努力使这项工作正常,但似乎我无法做到。

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

问题出在哪里?

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>

1 个答案:

答案 0 :(得分:3)

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