简体   繁体   English

wpf更改Button控件的IsEnabled属性和放在Button控件内部的TextBlock的Text属性

[英]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: 我在ListBoxItemDataTemplate有一个Button控件,其中有一个TextBlock

<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". 现在,当用户单击Button ,我想将按钮的IsEnabled属性更改为False ,并将TextBlockText更改为“ 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. 如果我们只是将Button换成ToggleButton并绑定到IsEnabledIsChecked本身。 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. 在使用反向布尔转换器将基于IsChecked的IsEnabled属性上的True交换为False时,反之亦然。 Since once you click it once, it's now disabled, the user won't be able to to uncheck the ToggleButton anyway. 由于单击一次后,它现在已被禁用,因此用户无论如何都无法取消选中ToggleButton。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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