简体   繁体   English

如何获取非绑定WPF组合框以显示选定的项目而不是{System.Windows.Controls.ComboBoxItem:

[英]How to get non-bound WPF combobox to show the selecteditem instead of {System.Windows.Controls.ComboBoxItem:

This has probably been answered somewhere, but I'm not sure how to phrase the question. 这可能已经在某处得到了回答,但是我不确定如何表达这个问题。

Is there a XAML only way to create a NON-BOUND WPF ComboBox that shows the actual SelectedItem (or SelectedValue) instead of "{System.Windows.Controls.ComboBoxItem: item 1}"? 是否有XAML唯一的方法来创建显示实际的SelectedItem(或SelectedValue)而不是“ {System.Windows.Controls.ComboBoxItem:项目1}”的非绑定WPF ComboBox? Right now, the selectedItem (or SelectedValue) requires splitting the string at the colon, then removing the last curly brace from the string. 现在,selectedItem(或SelectedValue)需要在冒号处分割字符串,然后从字符串中删除最后一个花括号。

Is there a way to do this without explicit code? 没有显式代码,有没有办法做到这一点?

                <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>

One nifty technique is to set the SelectedValuePath to either Content or Tag . 一种不错的技术是将SelectedValuePath设置为ContentTag Eg: 例如:

<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.

相关问题 &#39;无法将类型为&#39;System.String&#39;的对象转换为类型&#39;System.Windows.Controls.ComboBoxItem - 'Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ComboBoxItem ComboBox.SelectedItem不是ComboBoxItem - ComboBox.SelectedItem is not ComboBoxItem 如何以编程方式将 System.Windows.Controls.Parameter 绑定到 Silverlight 中的 ComboBox 的 SelectedItem - How to programmatically bind a System.Windows.Controls.Parameter to a ComboBox' SelectedItem in Silverlight 如何以编程方式设置数据绑定WPF ComboBox的SelectedItem? - How to programmatically set SelectedItem of a data-bound WPF ComboBox? WPF如何在具有不同项目源的列表视图中设置绑定组合框的selecteditem - WPF How to Set selecteditem of bound combobox in a listview with different itemsource 绑定到WPF组合框的“ SelectedItem”的属性的奇怪行为 - Weird behavior for a property bound to the 'SelectedItem' of a WPF Combobox 使用按钮更改绑定的WPF ComboBox SelectedItem - Changing a bound WPF ComboBox SelectedItem with a button 如何在 wpf 的 comboBox 中显示新选定项目后显示对话框? - How to show dialog after new selecteditem is shown in comboBox in wpf? WPF DataGrid,绑定到SortedList。 如何获得SelectedItem? - WPF DataGrid, bound to a SortedList. How to get SelectedItem? C# Windows Forms - 如何在 Combobox 中获取上一个 SelectedItem - C# Windows Forms - How to get previous SelectedItem in Combobox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM