user3611627
user3611627

Reputation: 11

Button disables itself in UserControl

Situation is simple: I have WPF UserControl of a WaitingBar witch has binded visibility. So problem is 'CancelButton' is always disabled when Control is set to visible. If i remove visibility binding control is always visible and button is enable. In overview of Waitingbar.xaml button is also enabled. What is wrong? Why button disables itself?

<UserControl x:Class="Neolant.Common.Ui.Wpf.Controls.WaitingBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:commonControls="clr-namespace:Neolant.Common.Ui.Wpf.Controls"
d:DataContext="{d:DesignInstance Type=commonControls:Waitor, IsDesignTimeCreatable=False}"
Visibility="{Binding Path=IsWaiting, Converter={StaticResource BooleanToVisibilityConverter}}"
IsHitTestVisible="False" >

<Grid
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    <!-- Фон -->
    <Rectangle 
        Grid.Row="0" Grid.RowSpan="2"
        Fill="LightGray" Opacity="0.5"
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

    <StackPanel
        Orientation="Vertical"
        HorizontalAlignment="Center" VerticalAlignment="Center">

        <!-- Анимация -->
        <Canvas
            Grid.Row="0"
            x:Name="canvas"
            HorizontalAlignment="Center" VerticalAlignment="Center"
            Height="100" Width="100"
            RenderTransformOrigin="0.5, 0.5">

            <Canvas.Triggers>
                <EventTrigger RoutedEvent="Canvas.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation 
                                Storyboard.TargetName="rotation" Storyboard.TargetProperty="Angle"
                                From="0" To="360" Duration="0:0:3" 
                                RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Canvas.Triggers>
            <Canvas.RenderTransform>
                <TransformGroup>
                    <RotateTransform x:Name="rotation" Angle="0"/>
                </TransformGroup>
            </Canvas.RenderTransform>
        </Canvas>
        <!-- Текст -->
        <TextBlock 
            Grid.Row="1"
            Text="{Binding Path=Text}"
            FontSize="20" 
            Foreground="SlateGray"
            Margin="5" 
            HorizontalAlignment="Center"/>
        <!-- Прогресс бар-->
        <ProgressBar
            Grid.Row="2"
            Height="25"
            Minimum="0"
            Maximum="100"
            Name="pbMain" 
            Visibility="Hidden" />
    </StackPanel>

    <!--Кнопка отмены-->
    <Button
        Grid.Row="0"
        x:Name="CancelButton" 
        Style="{StaticResource ApplicationButtonStyle}" 
        Width="50" 
        Height="50"
        Margin="0,0,0,62"
        IsEnabled="True">
        <Path Data="{StaticResource CloseIconGeometry}" />
    </Button>
</Grid>

Upvotes: 0

Views: 799

Answers (1)

Sheridan
Sheridan

Reputation: 69959

Ah... once again, somebody joins this website to ask yet another one of these questions that basically say 'Something in .NET doesn't work'. Meanwhile the truth of all of these questions is that while a very few things in .NET actually don't work... we can pretty much rely on them all working for the vast majority of the time.

Therefore, your answer is the same as all those that asked similar questions here... the Button does not disable itself in the UserControl (unless you disable the UserControl), because they don't do that... the only possible answer is that YOU have disabled it. It's hard to show you your mistake because you haven't included all of your relevant code (but please don't add any more).

I can hazard a guess that perhaps, you have set its IsEnabled property to false in a Loaded event handler or something similar. It can't be in the ApplicationButtonStyle because the setting the IsEnabled property to true on the Button would override that. If you have programmatically set the Button.Command property, then perhaps your CanExecute method is always returning false?

At the end of the day, this really is your mistake and your problem to fix. There is nothing that anybody here can do to help you with this further. You just need to look carefully through your project at what you do with this Button and at some stage, you'll find it. Just remember... Buttons just don't disable themselves by default.


UPDATE >>>

Well, you say that you've 'tried all this and it did not help', but as your problem still persists, you clearly haven't. I guarantee that it's your code that is disabling the Button. To prove this, just add another Button... clearly, it won't be disabled.

Try this instead... comment out the Button and try to build the project. If there are any references to the Button anywhere then you'll get an compilation error. If you get no compilation errors, then you probably have a parent container control with its IsEditable property set to True, thereby disabling all child controls.

Upvotes: 2

Related Questions