2013-03-22 18 views
0

工作我試圖通過下面的代碼來設置選定的項目,但它不工作:將selectedItem沒有在XAML

<StackPanel Orientation="Horizontal"> 
       <TextBlock Text="Sort by" Margin="10" VerticalAlignment="Center"/> 
       <ComboBox Width="{StaticResource ComboWidth}" x:Name="sortcombo" ItemsSource="{Binding Path=SortOrder}" SelectionChanged="SearchCombo_SelectionChanged" SelectedItem="{Binding Path=DefaultSortIndex}"> 
        <ComboBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding Path=Sort}"/> 
         </DataTemplate> 
        </ComboBox.ItemTemplate> 
       </ComboBox> 
      </StackPanel> 

不過,如果我使用的selectedIndex用,而不是綁定到第0指數正常工作。任何聲明錯誤?

+0

嘗試使您的selecteditem的綁定方式TwoWay – Natrium 2013-03-22 12:05:33

+0

它沒有工作。 – Ankit 2013-03-22 12:27:29

回答

0

發現了這個問題,其實數據源是創造新的列表,每次我打電話的getData()。

0

通過您的財產的名稱DefaultSortIndex也許您試圖綁定intSelectedItem

SeletedItem指的是您的集合中的元素綁定到ItemsSource,因此綁定到SelectedItem的屬性必須是您的集合元素的類型。

+0

沒有它只是一個快速的變化,我做了測試目的,我使用索引工作正常,但當嘗試更改選定項目的XAML(我也改變了屬性的類型),它沒有奏效。 – Ankit 2013-03-22 12:15:24

+0

當你說「它不工作」時,你是什麼意思? 1 /您在組合框中選擇一個項目,並且該值未在DefaultSortIndex屬性中設置? 2 /你的組合框中選擇了什麼? – polkduran 2013-03-22 12:24:21

+0

如果2 /您需要設置屬性DefaultSortIndex。 'DefaultSortIndex = SortOrder.FirstOrDefault();' – polkduran 2013-03-22 12:25:08

0

如果你將int值綁定到選定的項目,那麼它將不起作用,你應該爲此綁定元素。對於int值,你可以將其設置爲在下面的文章中提到:

Set Selected Item of WPF Combobox to User Setting

+0

爲什麼要投票? – 2013-03-22 12:12:39

+0

是的,我綁定了我用來綁定itemssource的類型的項目,我提到關於索引,bcz它工作正常,但是當我在xaml中更改時,我在Properties中進行了必要的更改 – Ankit 2013-03-22 12:13:23