user3952846
user3952846

Reputation: 141

WPF ComboBox selected item text

I made a custom combobox where I have a TextBlock (named mySelectedContent) to display the selected item and a TextBox for editing in "IsEditable" mode. I have a MultiDataTrigger that is being shot correctly, however, I am unable to "catch" the text of the selected item and put it into the TextBlock. How should be mounted the correct expression in place of "???". Thanks a lot!

Here is the code of the trigger (I'm showing mainly the part of the trigger because it's just in it the problem):

<ComboBox.Resources>    
    <Style x:Key="myComboBox" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <ToggleButton> 
                          ...
                        </ToggleButton>
                        <TextBlock
                          Name="mySelectedContent"
                          .../>
                        <TextBox x:Name="myEditableTextBox"
                          .../>
                        <Popup>
                          ...
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                ...
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter TargetName="myEditableTextBox" Property="Visibility" Value="Hidden"/>
                                <Setter TargetName="mySelectedContent" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="mySelectedContent" Property="Text" Value="???"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ComboBox.Resources>

Upvotes: 0

Views: 1117

Answers (2)

MattE
MattE

Reputation: 1114

That's way too much work when you simply could have used the Tag property to get the value easily with 2 lines:

in XAML:

<ComboBoxItem Content="This Value" Tag="This Value"/>

Then:

GetValue=ComboBoxName.SelectedItem.Tag.ToString()

will give you "This Value" and not "System.Windows.Controls.ComboBoxItem: This Value"

Much simpler, faster and less time consuming.

Upvotes: 0

user3952846
user3952846

Reputation: 141

It was solved with cYounes first suggestion. I used:

Value={Binding ElementName=MyEditableTextBox Path=Text}

and it works as expected!

Thanks!

Upvotes: 0

Related Questions