WPF: Convert integer to bool using ObjectDataProvider

1k views Asked by At

In my WPF app, I have a Save button which needs to be enabled when a collection count is greater than 0.

I am trying to convert using ObjectDataProvider which will use Convert.ToBoolean(int value). (I can use a converter, but why not try learn something different today.)

So I did as below, but it does not work.

<ObjectDataProvider x:Key="Convert"
                    ObjectType="{x:Type sys:Convert}"
                    MethodName="ToBoolean">
    <ObjectDataProvider.MethodParameters>
        <sys:Int32>0</sys:Int32>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<Button IsEnabled="{Binding MyCollection.Count, Source={StaticResource Convert}}">

What am i missing?

2

There are 2 answers

1
WPFUser On BEST ANSWER

Based on the link provided by Prjaval and you, i'm writing this as answer.

<Button IsEnabled="{Binding MyCollection.Count, Source={StaticResource Convert}}">

In your code, you are accessing, MyCollection.Count from object Boolean, so it will give binding errors and won't work.

we can achieve your requirement by, updating the method parameter of ObjectDataProvider via different source, and use the source in different binding. That means we cannot assign methodparameter and use the Source in same binding.

i tried like this and worked perfectly,

<Grid>
                <Grid.Resources>
                <ObjectDataProvider x:Key="convert"
                                    MethodName="ToBoolean"
                                    ObjectType="{x:Type sys:Convert}">
                    <ObjectDataProvider.MethodParameters>
                        <sys:Int32>0</sys:Int32>
                    </ObjectDataProvider.MethodParameters>
                </ObjectDataProvider>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <!--Updating Parameter-->
            <ItemsControl ItemsSource="{Binding Groups}">
                <i:Interaction.Behaviors>
                    <local:ObjectDataUpdate>
                        <local:ObjectDataUpdate.Count>
                            <Binding BindsDirectlyToSource="True"
                                         Mode="OneWayToSource"
                                         Path="MethodParameters[0]"
                                         Source="{StaticResource convert}" />
                        </local:ObjectDataUpdate.Count>
                    </local:ObjectDataUpdate>
                </i:Interaction.Behaviors>
            </ItemsControl>
            <!--Using ObjectDataProvider-->
            <Button Height="40" Grid.Row="1" IsEnabled="{Binding Source={StaticResource convert}}" />
        </Grid>

Behaviour

public class ObjectDataUpdate : Behavior<ItemsControl>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded += (_, __) =>
             {
                 //Some logics to update Count. I'm setting directly for sample purpose
                 Count = AssociatedObject.Items.Count;
             };
        }

        public int Count
        {
            get { return (int)GetValue(CountProperty); }
            set { SetValue(CountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count", typeof(int), typeof(ObjectDataUpdate), new PropertyMetadata(0));

    }

i used separate behaviour to update parameter.

please correct me if anything is wrong.

2
Prajwal On

I checked ObjectDataProvider and so far, I got this.

It dynamically creates an object to bind.

So, I think the problem is here.

"{Binding MyCollection.Count, Source={StaticResource Convert}}"

You need to bind only Source, and MyCollection.Count should be got in Convert.

Check out this tutorial from DevCurry.