简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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