簡體   English   中英

如何從ComboBox獲取屬性值?

[英]How to get property value from ComboBox?

我如何從中獲得財產:

 <ComboBox x:Name="cmbCategory" Grid.Column="1" Grid.Row="5" HorizontalAlignment="Center" PlaceholderText="Categories">
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <TextBlock TextWrapping="Wrap" Width="100%" Text="{Binding name}" />
             </StackPanel>
         </DataTemplate>
     </ComboBox.ItemTemplate>
 </ComboBox>

因為下面的代碼不起作用,因為我沒有'Content'屬性,所以我只有'name'屬性。 那么如何從name屬性中獲取價值?

string categories= (cmbCategory.Items[cmbCategory.SelectedIndex] as ComboBoxItem).Content.ToString();

我想到了。

我用過這個:

        public object GetPropertyValue(object car, string propertyName)
    {
        return car.GetType().GetProperties()
           .Single(pi => pi.Name == propertyName)
           .GetValue(car, null);
    }

來源: 如何根據名稱獲取屬性值

然后我就這樣使用它:

string categories= GetPropertyValue(cmbCategory.Items[cmbCategory.SelectedIndex], "name").ToString();

它有效,謝謝大家的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM