Click here to Skip to main content
15,117,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fairly new to WPF, and have tried almost everything to let me Set the checked state of a bound Checkbox in a WPF App I am writing to learn WPF more thoroughly.

I have tried endless different options from my web searches, including different StaticResource styles, many of which only half work in my testing, but while I can have the Checkbox jump through hoops when it s checked, I just cannot find a way to use the bool value it reads from a bound datasource so I can change the text alongside the Checkbox itself.

In other words, I do not want it to show True on loading, but rather "Yes" by somehow intercepting the value on loading and then replacing the Content, which is used for Binding at Design time, and display Yes or No as appropriate.

I am sure there is a simple answer, but I have tried for around 3 days now, (learning lots of other tricks on the way along), but with no success in setting this Checkbox's Content string.

What I have tried:

Trawled the web, trying lots of different suggestions, but they all relate to doing something when the Checkbox is clicked. I want to do something before, or rather during the load of the data form its bound data source (basically just a List)
Posted
Updated 16-Feb-21 23:20pm

If I've understood what you're trying to do, something like this should work:
XAML
<CheckBox>
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Setter Property="Content" Value="Not checked" />
            
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="Checked" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>
   
Comments
Maciej Los 16-Feb-21 13:19pm
   
5ed!
Thanks for quick response, but sadly, it doesn't fix my problem.

However, it did set me to experimenting a bit more, so I went thru every ISxxxxx option I could find, expecting to find something, and found the IsLoaded Property, and hoped I could use that, but it gives me an error that tells me you cannot reference that in XAML with "IsLoaded property is Read-only and cannot be set from Markup". But it pointed me to the Loaded Property and that allowed me to handle the problem.

So Loaded calls a Fn in Code behind and that function simply calls the same logic function I setup to run when the control is clicked, and it correctly set both the checking of the box and the text in Content.

I was rather confused by the fact that your Style only appears to handle the Value="True" scenario, so wondered how it handled the False situation, but clearly it is automatic in WPF

Anyway, it is now working as I want, and I more importantly, I understand How/Why, so many thanks indeed.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900