【问题标题】:How to bind Class Static Properties to View using MVVM and Silverlight 4如何使用 MVVM 和 Silverlight 4 将类静态属性绑定到视图
【发布时间】:2012-03-09 06:40:53
【问题描述】:

我创建了具有静态属性的类。现在我想将所有内容绑定到文本框。但我无法绑定。

具有静态属性的类:

namespace QSys.Library.Security
{
    public class CustomerServiceData
    {
        public static string UserName
        {
            get
            {
                return "Imdad";
            }
        }
    }
}

Page.xaml

<UserControl  x:Class="QSys.Admin.Views.AdminHomeView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:my="clr-namespace:QSys.Library.Security;assembly=QSys.Library"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="640">
    <UserControl.Resources>
        <my:CustomerServiceData  x:Name="mySecurity" />
    </UserControl.Resources>
    <Grid>
         <TextBox Text="{Binding Source={StaticResource mySecurity}, Path=CustomerServiceData.UserName}" ></TextBox>
    </Grid>
</UserControl>

谁能告诉我为什么我的文本框中没有得到值?即使它还没有给我任何错误。

谢谢, 伊姆达杜森

【问题讨论】:

    标签: silverlight data-binding mvvm


    【解决方案1】:

    您在那里指定了错误的路径,另一件事总是为您添加的资源指定一个键。以下是您需要进行的更改才能使其工作:

     <UserControl.Resources> 
    <my:CustomerServiceData  x:Key="mySecurity" />
     </UserControl.Resources>
    

    文本框默认为双向绑定,因此如果您不将其设置为 OneWay,您将收到错误消息,因为由于其双向绑定行为,它还会在您定义的属性中查找 setter。

    <TextBox Text="{Binding Source={StaticResource mySecurity},Mode=OneWay, Path=UserName}" >
    </TextBox>
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2017-07-10
      • 2010-12-21
      • 2016-01-06
      • 2014-09-10
      • 1970-01-01
      • 2012-07-11
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多