简体   繁体   English

WPF DataTrigger不起作用

[英]Wpf DataTrigger not working

I have a button where is enabled property should happen twice. 我有一个按钮,其中enabled属性应该发生两次。 In the viewmodel if the IsEnabled property is set to false the button should be disabled which works fine. 在视图模型中,如果IsEnabled属性设置为false,则应该禁用该按钮,这样可以很好地工作。 On the other hand when the Validate button in the UI is disabled, this button should also be disabled that is not working which indicates that the Data Trigger is not working. 另一方面,当禁用UI中的“验证”按钮时,也应禁用该按钮,该按钮不起作用,这表示数据触发器不起作用。 Please help. 请帮忙。

<Button x:Name="BtnValidate" Content="Validate" Height="24" VerticalAlignment="Top" Grid.Column="2" Width="83" Command="{Binding ValidateCommand}" IsEnabled="{Binding IsValidateEnabled}" HorizontalAlignment="Left" Margin="8,28,0,0" />
    <Button Name="BtnReload" IsEnabled="{Binding IsEnabled}" HorizontalAlignment="Left" Width="123" Grid.Row="0" Grid.Column="5" Content="Reload" Command="{Binding DateCommand}" Margin="8,24,0,32">
                        <Button.Style>
                            <Style TargetType="Button">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=BtnValidate,Path=IsEnabled}" Value="False">
                                        <Setter Property="IsEnabled" Value="False"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>

Properites set in the <Tag> of the object take precendence over any property set in a <Style> , so in your case the IsEnabled="{Binding IsEnabled}" is always going to be used. 在对象的<Tag>中设置的属性优先于在<Style>设置的任何属性,因此在您的情况下,将始终使用IsEnabled="{Binding IsEnabled}" See MSDN's Dependency Property Precedence List for more information. 有关更多信息,请参见MSDN的依赖项属性优先级列表

The solution would be to move the IsEnabled property out of the tag definition and into the style, since properties set in a Trigger take precedence over properties set in the <Style> 解决方案是将IsEnabled属性移出标签定义并移至样式中,因为在Trigger中设置的属性优先于在<Style>设置的属性。

<Button Name="BtnReload" Content="Reload" Command="{Binding DateCommand}">
    <Button.Style>
        <Style TargetType="Button">
           <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=BtnValidate,Path=IsEnabled}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

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

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