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