简体   繁体   中英

XAML (WPF) property has no accessible setter to bool value

There is probably just some silly mistake, but i can't seem to find it. The problem is: the compiler says The property "IsMarried" does not have an accessible setter and i don't know what it means by "accessible", because there is definately a setter there.

XAML:

<Window x:Class="BindingTests.MainWindow"
...
    xmlns:cnsmr="clr-namespace:BindingTests;assembly=BindingTests">

<Window.Resources>
    <cnsmr:CustomerViewModel x:Key="CustomerViewModel" ... IsMarried="true"/>
</Window.Resources>

Customer.cs:

namespace BindingTests
{
    class Customer
    {   
        ...
        public string Married { get; set; }
        ...
    }
}

CustomerViewModel.cs

namespace BindingTests
{
    class CustomerViewModel
    {
        private Customer obj = new Customer();
        ...
        public bool IsMarried
        {
            get
            {
                if (obj.Married == "Married")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                if (value)
                {
                    obj.Married = "Married";
                }
                else
                {
                    obj.Married = "Not Married";
                }
            }

        }
    ...

Just to clarify, i can't see how this setter differs from another existing setter, which rises no errors: also CustomerViewModel.cs:

public string TxtCustomerName
        {
            get { return obj.CustomerName; }
            set { obj.CustomerName = value; }
        }

I knew it was some silly mistake. The problem was this string in XAML:

xmlns:cnsmr="clr-namespace:BindingTests;assembly=BindingTests"

I got it from some tutorial, and just blindly copied. Removed the ";assembly=BindingTests" part and now everything is fine. Now XAML looks like this:

xmlns:cnsmr="clr-namespace:BindingTests"

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