![](/img/trans.png)
[英]'Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ComboBoxItem
[英]How to get non-bound WPF combobox to show the selecteditem instead of {System.Windows.Controls.ComboBoxItem:
這可能已經在某處得到了回答,但是我不確定如何表達這個問題。
是否有XAML唯一的方法來創建顯示實際的SelectedItem(或SelectedValue)而不是“ {System.Windows.Controls.ComboBoxItem:項目1}”的非綁定WPF ComboBox? 現在,selectedItem(或SelectedValue)需要在冒號處分割字符串,然后從字符串中刪除最后一個花括號。
沒有顯式代碼,有沒有辦法做到這一點?
<ComboBox x:Name="cboTilePattern" Height="22" Width="200" Margin="0,0,20,0" >
<ComboBoxItem IsSelected="True">Square: Quarter Turn</ComboBoxItem>
<ComboBoxItem>Square: Monolithic Horizontal</ComboBoxItem>
<ComboBoxItem>Square: Monolithic Vertical</ComboBoxItem>
<ComboBoxItem>Rectangle: Chevron</ComboBoxItem>
<ComboBoxItem>Rectangle: Brick Horizontal</ComboBoxItem>
<ComboBoxItem>Rectangle: Brick Horizontal Flip</ComboBoxItem>
<ComboBoxItem>Rectangle: Brick Vertical</ComboBoxItem>
<ComboBoxItem>Rectangle: Brick Vertical Flip</ComboBoxItem>
<ComboBoxItem>Rectangle: Monolithic Horizontal</ComboBoxItem>
<ComboBoxItem>Rectangle: Monolithic Vertical</ComboBoxItem>
<ComboBoxItem>2 Squares: Checkerboard</ComboBoxItem>
<ComboBoxItem>2 Squares: Quarterturn Checkerboard</ComboBoxItem>
</ComboBox>
一種不錯的技術是將SelectedValuePath
設置為Content
或Tag
。 例如:
<StackPanel>
<ComboBox x:Name="combo" SelectedValuePath="Content">
<ComboBoxItem Content="item 1" />
<ComboBoxItem Content="item 2" />
</ComboBox>
<TextBlock Text="{Binding ElementName=combo,Path=SelectedValue}" />
</StackPanel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.