Databinding on a IValueConverter
Asked Answered
P

3

7

Does anybody know if it is possible to do databinding on an IValueConverter based class?

I have the following converter:

[ValueConversion(typeof(int), typeof(Article))]
public class LookupArticleConverter : FrameworkElement, IValueConverter {
    public static readonly DependencyProperty ArticlesProperty = DependencyProperty.Register("Articles", typeof(IEnumerable<Article>), typeof(LookupArticleConverter)); 

    public IEnumerable<Article> Articles
    {
        get { return (IEnumerable<Article>)GetValue(ArticlesProperty); }
        set { SetValue(ArticlesProperty, value); }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        ...
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        ...
    }
}

It's purpose is to lookup an article in a list by its Id, and return that article.

However, I'd like to fill the Articles property by databinding a collection to it, like this:

<local:LookupArticleConverter Articles="{Binding Articles}" x:Key="LookupArticle"/>

But this doesn't seem to work. The setter method is never called. The source property contains an actual nonempty collection, so that's not the problem.

There's no error messages regarding binding in the output log, neither.

Any clues?

Pacien answered 9/3, 2009 at 9:22 Comment(1)
codeproject.com/Articles/71348/…Sashenka
K
3

WPF doesn't really support this intrinsically. However, there are some techniques that will allow you to do this, including Josh Smith's virtual branch approach.

Kero answered 9/3, 2009 at 9:31 Comment(0)
U
10

So, the problem is that resources are not part of the visual tree. To make this work you have to:

1. Make your ValueConverter inherit Freezable

 public class CustomConverter : Freezable, IValueConverter
 {

    public static readonly DependencyProperty LookupItemsProperty =
        DependencyProperty.Register("LookupItems", typeof(IEnumerable<LookupItem>), typeof(CustomConverter), new PropertyMetadata(default(IEnumerable<LookupItem>)));

    public IEnumerable<LookupItem> LookupItems
    {
        get { return (IEnumerable<LookupItem>)GetValue(LookupItemsProperty); }
        set { SetValue(LookupItemsProperty, value); }
    }

    #region Overrides of Freezable

    /// <summary>
    /// When implemented in a derived class, creates a new instance of the <see cref="T:System.Windows.Freezable"/> derived class.
    /// </summary>
    /// <returns>
    /// The new instance.
    /// </returns>
    protected override Freezable CreateInstanceCore()
    {
        return new CustomConverter();
    }

    #endregion Overrides of Freezable

    // ... Usual IValueConverter stuff ...

}

2. Bind to the visual tree using Binding ElementName

<UserControl (...) x:Name=myUserControl> 
<UserControl.Resources>
<CustomConverter x:Key="customConverter"
                    LookupItems="{Binding ElementName=myUserControl, Path=DataContext.LookupItems}"/>
</UserControl.Resources>
Upend answered 18/12, 2011 at 4:22 Comment(1)
I created it but the LookupItems doesn't bind and the CreateInstanceCore() doesn't execute as I put break pointsSashenka
K
3

WPF doesn't really support this intrinsically. However, there are some techniques that will allow you to do this, including Josh Smith's virtual branch approach.

Kero answered 9/3, 2009 at 9:31 Comment(0)
B
0

I've added the RelativeSource method to the binding statement. This resolve the issue where I have my IValueConverter defined under the Control.Resources.ResourceDictionary tree, and i'm attempting to get the DataContext from the Control.DataContext

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/Text.xaml"/>
            <ResourceDictionary Source="/Resources/Layout.xaml"/>
            <ResourceDictionary Source="/Resources/Colours.xaml"/>
            <ResourceDictionary Source="/Resources/Icons.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <conv:OrientationConverter x:Key="OerientationConverter" IsOrientationRuleEnabled="{Binding Path=DataContext.IsSiteLayoutPhysical, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"/>
....
<ListView x:Name="operationsListView" ItemsSource="{Binding .}" DataContext="{Binding GroupedPlantItems}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Panel.ZIndex="-10">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.Panel >
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="{Binding Name, Converter={StaticResource OerientationConverter}}"/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <GroupBox Header="{Binding Name}">
                                            <ItemsPresenter/>
                                        </GroupBox>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

I'm being tricky here and changing the way a StackPanel orientates its content based upon some rules defined within an IValueConverter. I use the binding on the converter to disable or enable the rules from my ViewModel. I could also set the rules by this method, or perhaps pass it a lambda expression for validation. please excuse my typos.

Baten answered 5/1, 2012 at 3:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.