简体   繁体   English

ComboBox的默认值?

[英]ComboBox default value?

I'm writing a Windows Store app and I need a ComboBox to have its default value. 我正在编写Windows应用商店应用,我需要一个ComboBox才能具有其默认值。 I also would like to know which item from the list the user selects but I can't find out how to do it. 我也想知道用户从列表中选择了哪个项目,但我不知道该怎么做。 I tried different properties but with no results. 我尝试了其他属性,但没有结果。 Any ideas about doing this? 有任何想法吗?

The code I have for create the ComboBox is: 我创建ComboBox的代码是:

<ComboBox x:Name="cboxelemento" Width="350" ItemsSource="{Binding}"
        SelectionChanged="cboxelemento_SelectionChanged">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock x:Name="lnombre" Text="{Binding Nombre}" FontSize="24"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.DataContext>
        <Clases:Datos/>
    </ComboBox.DataContext>
</ComboBox>

First you must use the SelectedValueChanged Event. 首先,您必须使用SelectedValueChanged事件。

 private void cmbox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbox.Focused)
               {
                  //do

               }
        }

The property Focused goes to true when you have clicked on the ComboBox. 当您单击ComboBox时,属性Focused变为true。

private void cmbox_SelectedValueChanged(object sender, EventArgs e)
{
   var val = cmbox.SelectedValue;
   //or
   //cmbox.Selectedindex;
}

you can access the value like this 您可以像这样访问值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM