1

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?

7
  • 1
    I would swap your Button for ToggleButton and bind your isenabled to ischecked, and you don't really need the TextBlock in there it looks like. Commented Jan 4, 2016 at 15:10
  • @Chris If i change it with Tooglebutton, does that mean if i click it once again, the button will be enabled again? if so, then i dont want it :/ Commented Jan 4, 2016 at 15:20
  • 1
    Not unless you manually revert back the bool on IsEnabled. It would be something like <ToggleButton IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource Self}, Converter="{StaticResource InvertedBoolConverter"}}"/> So once it's clicked, that's it, it's disabled, until it is somehow reset be it manually or if a screen re-renders or something. Not sure of your requirements but you could save the bool if it needs to stay persistent. Your Inverted bool converter on it will just make it so IF IsChecked=True, THEN IsEnabled=False Commented Jan 4, 2016 at 15:25
  • @ChrisW. Im still not familiar yet with Converter stuff. I wrote "if ((bool)value == true) { return false; } return true;" When i click the button an error occured :( Commented Jan 4, 2016 at 15:50
  • Give "wpf inverted boolean converter" a quick google, there's tons of tutorials. :) Commented Jan 4, 2016 at 15:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

you said that it is possible to save the boolean value, how?
Another quick google search for something like "saving settings" or "saving values" for wpf should give you some tutorials on how to do like Blah.Save() and show you some best practices. :)
Oh and of course if you run into specific issues implementing it, just ask a separate question on here. Lots of folks ready to help!
Yea I think i will do that but not now. I will try it by myself first :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.