Wahyu
Wahyu

Reputation: 5061

wpf Change IsEnabled property of Button control and Text property of TextBlock which placed inside the Button control

I have a Button control inside a DataTemplate for my ListBoxItem with a TextBlock inside it:

<DataTemplate x:Key="SomeitemTempate">
....
<Button x:Name="nxBut" Margin="20,0,20,20" Height="30" DockPanel.Dock="Bottom" FontSize="16" Background="White" Click="nxBut_Click">
     <TextBlock x:Name="nxBut_Txt" Text="Some Button" FontWeight="Bold" Foreground="#FF11700C"/>
</Button>
....

Now when user clicked the Button, I want to change IsEnabled property of the button to False, and the Text of the TextBlock to "Clicked". After googling for some times, i modified it:

    ....
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding ElementName=nxBut, Path=IsPressed}" Value="True">
            <Setter TargetName="nxBut" Property="IsEnabled" Value="False" />
            <Setter TargetName="nxBut_Txt" Property="Text" Value="Clicked"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

But it does not work. What am I doing wrong?

Upvotes: 1

Views: 741

Answers (1)

Chris W.
Chris W.

Reputation: 23290

So like discussed. If we just swap Button for ToggleButton and bind to IsEnabled to IsChecked of itself. Then upon press it will just disable itself until it's manually reset by other means. Something like;

<ToggleButton IsEnabled="{Binding IsChecked, 
                                  RelativeSource={RelativeSource Self},
                                  Converter="{StaticResource InvertedBoolConverter"}}"/>

While using an inverted boolean converter to swap True for False or vice versa on the IsEnabled property based on IsChecked. Since once you click it once, it's now disabled, the user won't be able to to uncheck the ToggleButton anyway. Hope this helps.

Upvotes: 1

Related Questions