简体   繁体   English

XAML(WPF)属性没有布尔值可访问的设置器

[英]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. 问题是:编译器说属性“ IsMarried”没有可访问的设置器 ,我也不知道“可访问”的含义,因为那里肯定有一个设置器。

XAML: 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: Customer.cs:

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

CustomerViewModel.cs 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: 为了澄清起见,我看不到此设置器与另一个现有设置器的区别,后者没有出现任何错误:还有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: 问题是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. 删除了“; assembly = BindingTests”部分,现在一切正常。 Now XAML looks like this: 现在XAML看起来像这样:

xmlns:cnsmr="clr-namespace:BindingTests"

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

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