[英]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.