Risho
Risho

Reputation: 2647

Setting/Returning value in a property of a WPF application

While this may not be a WPF exclusive issue, I'm rather new to the technology so I'm not certain how to solve this. I need to set value to a property based on the status of the check box. Prior to work the property looks like this and is located in different project/class in the solution:

[XmlElement(ElementName = "MyElement_A")]
public bool MyElement_A { get; set; }

There is file MainWIndows.xaml where the control is created <CheckBox Content="Check if Yes" Name="checkBox1"/>

So I thought that perhaps something like this would work but I'm out of context and the set also has an error "must declare a body because it is not marked abstract, extern, or partial":

[XmlElement(ElementName = "MyElement_A")]
public bool MyElement_A {
    get
    {
        return (bool)checkBox1.IsChecked.Value;
    }
    set
    {
        return;
    }
}

I know in "web" world you have to use .FindControl, I thought in WPF, state isn't an issue. What I'm asking is how do you get the value of the check box and assign it to the MyElement_A?

Upvotes: 1

Views: 169

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61349

This is what binding is for.

You should have this:

<CheckBox Content="Check if Yes" IsChecked="{Binding MyElement_A }"/>

Of course, that will only work if the class containing MyElement_A is the DataContext for the view. Judging by your code, I would strongly reccommend looking into learning the MVVM pattern and how to use it with WPF. You will find that stuff like this becomes a lot easier.

Upvotes: 3

Related Questions