根据 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));
}
我使用单独的行为来更新参数。
如果有任何问题,请纠正我。