简体   繁体   English

奇怪的silverlight绑定问题

[英]Weird silverlight binding issue

I have a weird problem. 我有一个奇怪的问题。 I have two stack panels, with a button in each, the data context of both stack panels are the same and the IsEnabled property on the buttons are also set to the same property. 我有两个堆栈面板,每个堆栈面板都有一个按钮,两个堆栈面板的数据上下文相同,按钮上的IsEnabled属性也设置为相同的属性。 Yet one button is disabled and the other is enabled: 然而,一个按钮被禁用,另一个按钮被启用:

StackPanel #1 (Enabled): StackPanel#1(启用):

<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" Height="23"  DataContext="{Binding ElementName=LayoutRoot, Path=DataContext}">
    <TextBox Grid.Row="0" Grid.Column="1" Height="23" Width="220" Name="txtBarcode"></TextBox>
    <Button IsEnabled="{Binding CanAdd}" cal:Click.Command="{Binding AddBarcodeCommand}" Width="40" Content=". . ." Margin="5,0"></Button>
</StackPanel>

StackPanel #2 (Disabled): StackPanel#2(已禁用):

<StackPanel Orientation="Horizontal" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right"  DataContext="{Binding ElementName=LayoutRoot, Path=DataContext}" >
    <Button HorizontalAlignment="Right" cal:Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.SaveCommand}" Grid.Row="3" Grid.Column="1" Height="30" Content="Create Batch" Width="130"></Button>
    <Button IsEnabled="{Binding CanAdd}" Height="30" Content="Apply" Width="130" HorizontalAlignment="Right" Margin="5,0"></Button>
</StackPanel>

I did not see any databinding errors in the output window. 我没有在输出窗口中看到任何数据绑定错误。 Is there something that I am missing? 有什么东西我错过了吗?

It's probably because of your command in the first Button: 这可能是因为你在第一个Button中的命令:

cal:Click.Command="{Binding AddBarcodeCommand}

If that command has a CanExecute method defined for it, it will disable the button when that method returns false. 如果该命令具有为其定义的CanExecute方法,则在该方法返回false时将禁用该按钮。

It may also apply to SaveCommand 它也可能适用于SaveCommand

I would remove the IsEnabled binding and let the CanExecute do its thing as it's the point of commands to do so, 我会删除IsEnabled绑定并让CanExecute执行它的操作,因为它是命令点,
but you can also fix the CanExecute logic or remove it and let the IsEnabled binding work. 但您也可以修复 CanExecute逻辑或删除它并让IsEnabled绑定工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM