Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I have following xaml code where I am trying to bind converterParameter value:
C#
<Window.Resources>
    <ResourceDictionary>
        <local:ExpandedCategoryConverter x:Key="ExpandConverter"/>
        <Style TargetType="{x:Type propgrid:PropertyGridDataAccessorItem}">
            <Style.Triggers>
                <Trigger Property="DataAccessorType" Value="Category">
                    <Setter Property="IsExpanded">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource ExpandConverter}">
                                <Binding Path="DisplayName"/>
                                <Binding Path="ExpandCategory"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>  
    </ResourceDictionary>
</Window.Resources>


The problem is that I want to bind ExpandCategory not from TargetType="{x:Type propgrid:PropertyGridDataAccessorItem}", but from my viewModel.
How can I do that?
Thnx in advance
Posted
Comments
koleraba 4-Jun-13 8:24am    
Binding expression which defines just the 'Path' attribute always binds to DataContext of the current instnace. So as long DataContext of your PropertyGridDataAccessorItem contains the correct view-model this should work.
Alexander Dymshyts 4-Jun-13 8:37am    
In my converter class in debug mode I get DisplayName fine, but ExpandCategory is - unset({DependencyProperty.UnsetValue}).
Any ideas how can I fix it?
koleraba 4-Jun-13 22:26pm    
Do you get any notifications about failed bindings in the output window when you run the solution?
Alexander Dymshyts 5-Jun-13 3:00am    
Yes, I have an error "Cannot find source for binding with reference 'ElementName=window'". I think it's because element ExpandCategory defined in my viewModel, and TargetType for my style is PropertyGridDataAccessorItem, so there are no such element ExpandCategory in PropertyGridDataAccessorItem. But how to solve this I have no idea right now
koleraba 5-Jun-13 11:06am    
Sorry I can not say more without seeing more of the code, but it seems to me there is a problem how your binding are set up. If you use Binding ElementName=window it means that you do not bind to your data context but to another element in your visual tree. Try to find the reason for your failed binding and than go on from there.
Uros

1 solution

The solution was very simple - I just added RelativeSource to my binding in Mode = FindAncestor.
Code looks now so:

C#
<window.resources>
    <resourcedictionary>
        <style targettype="{x:Type propgrid:PropertyGridDataAccessorItem}">
            <style.triggers>
                <trigger property="DataAccessorType" value="Category">
                    <setter property="IsExpanded">
                        <setter.value>
                            <multibinding>
                                <multibinding.converter>
                                    <local:expandedcategoryconverter  />
                                </multibinding.converter>
                                <binding path="DisplayName" relativesource="{RelativeSource Self}" />
                                <binding path="ViewModel" relativesource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" />
                            </multibinding>
                        </setter.value>
                    </setter>
                </trigger>
            </style.triggers>
        </style>  
    </resourcedictionary>
</window.resources>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900