3

在我的 WPF 应用程序中,我有一个保存按钮,当集合计数大于 0 时需要启用该按钮。

我正在尝试使用 ObjectDataProvider 进行转换,它将使用Convert.ToBoolean(int value). (我可以使用转换器,但为什么不今天尝试学习一些不同的东西。)

所以我做了如下,但它不起作用。

<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}}">

我错过了什么?

4

2 回答 2

1

根据 Prjaval 和您提供的链接,我将其写为答案。

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

在您的代码中,您正在从对象布尔访问 MyCollection.Count,因此它会给出绑定错误并且不起作用。

我们可以通过不同的源更新 ObjectDataProvider 的方法参数,并在不同的绑定中使用源来实现您的要求。这意味着我们不能分配方法参数并在同一绑定中使用 Source。

我试过这样并且工作得很好,

<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>

行为

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));

    }

我使用单独的行为来更新参数。

如果有任何问题,请纠正我。

于 2016-12-19T10:13:43.193 回答
0

我检查了ObjectDataProvider,到目前为止,我得到了这个。

它动态创建要绑定的对象。

所以,我认为问题就在这里。

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

您只需要绑定 Source,并且MyCollection.Count应该在Convert.

从DevCurry查看本教程。

于 2016-12-19T06:41:04.950 回答