繁体   English   中英

在组合框中设置XAML中的默认值

[英]Setting a Default value in XAML in combobox

我正在尝试为组合框设置默认值。 我有创建代码表的脚本,所以编写这样的代码不是一个选项:

<StackPanel>
<ComboBox SelectedValue="CA">
    <ComboBoxItem Tag="CO">Colorado</ComboBoxItem>
    <ComboBoxItem Tag="CA">California</ComboBoxItem>
    <ComboBoxItem Tag="NM">New Mexico</ComboBoxItem>
</ComboBox>
</StackPanel>

相反,我在XAML中有一些代码:

 <StackPanel HorizontalAlignment="Left" Margin="10,5,0,0" Name="PersonStackPanel" Height="60">
            <Label VerticalContentAlignment="Top" FontWeight="Normal" Height="28" HorizontalAlignment="Left">Person</Label>
            <ComboBox Name="PersonComboBox"  Width="312" Props.CodeProvider="MASTCODE.TYPE.ARRA" DisplayMemberPath="NAME" Tag="AA" IsSelected="True" SelectedValuePath="CODE" SelectedItem="{Binding Path=PERSON}"/>
 <StackPanel>

我认为Tag =“AA”IsSelected =“True”会起作用,但事实并非如此。

组合框中显示的任何值都必须作为选择存在。

尝试:

<ComboBox SelectedIndex="0"/>    

编辑:添加报价

您正在使用

SelectedValuePath="CODE"

所以在这种情况下尝试设置SelectedValue而不是SelectedItem

在ViewModel中将Person设置为适当的值(如果您想要默认项,则可能是构造函数)。

Person = //some object 

并将Binding更改为

SelectedItem="{Binding Path=PERSON, Mode=TwoWay}"

谢谢你们,我想这会做:

    <ComboBox Name="PersonComboBox"  Width="312" SelectedItem="{Binding Path=PERSON}" SelectedIndex="0" >
                <ComboBoxItem Tag="AA">Adam</ComboBoxItem>
            </ComboBox>

我觉得我很困惑的是,因为我有一个创建组合框项目的脚本,我不需要在XAML中创建它。 但只要我能将这些数据保存到“人物”栏目中,我就会很好。

暂无
暂无

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

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