I can't change property of a button....

c00012 741 Reputation points
2024-10-11T04:31:36+00:00

Hello,

I wrote an app has 1 datepicker, 2 textblocks, 2 textboxes, and 2 buttons.

My problem is one of the buttons remains unabled like below.

app error

xaml code for this view is:


<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        xmlns:model ="clr-namespace:WpfApp3.Model"
        xmlns:viewModel ="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Background="AliceBlue"
        ResizeMode="NoResize"
        Title="MainWindow" Height="400" Width="350">
    <Window.DataContext>
        <viewModel:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1.5*"/>
            <RowDefinition Height="1.5*"/>
            <RowDefinition Height="1.5*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="1.8*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" x:Name="Date" Text="Date:" FontSize="20" FontWeight="Bold"  Margin="10,10,10,10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <DatePicker x:Name="getdate" SelectedDate="{Binding SelectedDate}" Grid.Column="1" Width="200" Height="20" Margin="10,10,10,10" />
        </Grid>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="1.8*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" x:Name="Item" Text="Item:" FontSize="20" FontWeight="Bold" Margin="10,40,10,40" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox Grid.Column="1" FontSize="14" Text="{Binding Item}" Margin="10,40,10,40"/>
        </Grid>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="1.8*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" x:Name="Amount" Text="Amount:" FontSize="20" FontWeight="Bold" Margin="10,40,10,40" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox Grid.Column="1" FontSize="14" Text="{Binding Amount}"   Margin="10,40,10,40"/>
        </Grid>
        <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Content="ShowInput" Command="{Binding getInputCommand}" FontSize="14" FontWeight="Bold" Margin="20,10,20,10"/>
            <Button Grid.Column="1" Content="Clear" FontSize="14" Command="{Binding ClearCommand}" FontWeight="Bold" Margin="20,10,20,10"/>
 
        </Grid>
    </Grid>
</Window>

I changed "isEnabled" property for the button several times and saved it but these trials didn't work.

I didn't experience case like this before, so I don't know how can i fix it.

if anyone give me an advice for this, I'd be very appreciated.

thanks for your help in advance.

c00012

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,786 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,032 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 46,556 Reputation points Microsoft Vendor
    2024-10-11T05:11:53.9366667+00:00

    Hi @c00012 , Welcome to Microsoft Q&A,

    There is nothing wrong with your code, except that you may have manually set the "isEnabled=false" of the button in your code-behind page.

    To ensure that the problem is not with the command logic, you can temporarily remove the Command binding from the button and see if it is enabled:

    I suggest you delete this button and add it again to try.

    If you mean it cannot be enabled in runtime.

    It may be because you are using button commands ("getInputCommand" and "ClearCommand"), please make sure the "CanExecute" logic of these commands allows the button to be enabled. If the "CanExecute" method returns "false", the button will remain disabled. In your ViewModel, make sure the "CanExecute" method of both commands is defined correctly and returns "true" when it should.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.