Kynethix
Kynethix

Reputation: 147

Can't change ComboBox selected item

I've been all day trying to make this work properly, but it seems I'm unable.

The first thing I do is populate a combobox with the list of voices installed on system. Once that the combobox is properly populated with all the voices, I can't select any value from the combobox.

Where is the problem?

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";
}

And this is the XAML

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

All the XAML related to my problem:

<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>

Upvotes: 1

Views: 2548

Answers (1)

Curtis
Curtis

Reputation: 5892

Is accSettings() being called more than once? Put a breakpoint there and see. If you click an item, and then accSettings() gets called again, the combobox will be reset to its initial values and your selection will be lost.

Upvotes: 3

Related Questions