简体   繁体   中英

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}"? Right now, the selectedItem (or SelectedValue) requires splitting the string at the colon, then removing the last curly brace from the string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM