我有一個 ComboBox 我需要這樣做:
<ComboBox x:Name="CitySelectComboBox" SelectedValuePath="Text">
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<b:InvokeCommandAction Command="{Binding DownloadWeatherCommand}"
CommandParameter="{Binding ElementName=CitySelectComboBox, Path=SelectedValue}" />
</b:EventTrigger>
<b:EventTrigger EventName="SelectionChanged">
<b:InvokeCommandAction Command="{Binding DownloadWeatherCommand}"
CommandParameter="{Binding ElementName=CitySelectComboBox, Path=SelectedValue}" />
</b:EventTrigger>
</b:Interaction.Triggers>
<ComboBoxItem IsSelected="True">
<TextBlock Text="Praha"/>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="Brno"/>
</ComboBoxItem>
</ComboBox>
如您所見,我正在嘗試訪問Text. 只是 put不起作用,因為我收到以下錯誤:TextblockComboBoxItemText
BindingExpression path error: 'Text' property not found on 'object' ''ComboBoxItem' (Name='')'.
我需要了解如何在 中指定此屬性的路徑,SelectedValuePath但沒有任何在線資源有幫助。如果我在ComboBoxItemas 中包含城市名稱,則代碼可以正常作業<ComboBoxItem Content="Praha"/>,但我需要從子元素中獲取名稱TextBlock。
uj5u.com熱心網友回復:
SelectedValuePath以 為目標SelectedItem,在您的情況下是 a ComboBoxItem。ComboBoxItem沒有Text財產。
TextBlock分配給屬性ComboBoxItem.Content。
因此,有效的屬性路徑是"Content.Text":
<ComboBox SelectedValuePath="Content.Text">
<ComboBoxItem IsSelected="True">
<TextBlock Text="Praha" />
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="Brno" />
</ComboBoxItem>
</ComboBox>
因為如果值是 a ComboBoxItem(或者ContentPresenter準確地說)將TextBlock隱式創建 a ,所以顯式定義 a是多余的。
而是直接設定屬性。這減少了代碼并增強了代碼的可讀性:ContentstringTextBlockContent
<ComboBox SelectedValuePath="Content">
<ComboBoxItem IsSelected="True"
Content="Praha" />
<!-- Alternative content property syntax -->
<ComboBoxItem>Brno</ComboBoxItem>
</ComboBox>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/528382.html
標籤:C#wpf
