D J
D J

Reputation: 7028

how to pass current Combobox item in converter in a style

I am trying to create a style for comboxitem. I want to pass the current comboboxitem to converter. Style is like

 <Style x:Key="MyVisibilityStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Visibility">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource VisibiltyMultiValueConverter}">
                    <Binding Path="."/>
                    <Binding Path="SelectedItem"
                             ElementName="ABCComboBox"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>

Problem is the "." is passing the object of MainWindow not the comboboxitem.

Upvotes: 0

Views: 1463

Answers (2)

Florian Gl
Florian Gl

Reputation: 6014

Through <Binding Path="."> youre passing the object which the ComboBoxItem holds, but with <Binding RelativeSource="{RelativeSource Self}"/> you can pass the control itself.

What you also could do is passing the whole ComboBox and its selected index/item:

and in your converter you could get your ComboBoxItem like so:

ComboBoxItem cbi = (ComboBoxItem)(cb.ItemContainerGenerator.ContainerFromIndex(selectedindex));

or

ComboBoxItem cbi = (ComboBoxItem)(cb.ItemContainerGenerator.ContainerFromItem(selecteditem));

Upvotes: 2

user1064519
user1064519

Reputation: 2190

You can get the selected item of the combobox by using FindAncestor:

<Binding Path="SelectedItem" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ComboBox}"/>

Upvotes: 1

Related Questions