开发者

how to hide a button that is bound to a command that cannot execute?

Instead of disabling the button which happens automatically, i would like to hide (or rather c开发者_如何学编程ollapse the visibility of) the button.


You could use a Style and Triggers, assuming that the command is in charge of setting the Button enabled/disabled:

        <Button x:Name="btnMoveUp"
                Command="{x:Static local:Window1.MoveItemUp}">
            <Button.Style>
                <Style TargetType="{x:Type Button}" >
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

Note that you can define this Style at a higher scope and share it - I just put it right with the Button for a more compressed example.


The same behavior without style and trigger, if the Visibility property is not bound yet.

Command={Binding MyCommand}
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BTVC}}"

Where BTVC is a BooleanToVisibilityConverter (that is a must have).


Use the BooleanToVisibilityConverter and bind to a bool as described here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜