小编tho*_*ill的帖子

带有MVVM和DataAnnotations的WPF,UserControl中的验证错误

我有一个UserControl,将在我们正在开发的应用程序中重用.我们正在使用基于MVVMLight的框架.

为简单起见,假设用户控件只包含一个文本框,并公开一个名为"Quantity"的依赖项属性.用户控件上的文本框数据绑定到依赖项属性"Quantity".

在视图上使用用户控件时,usercontrol的"Quantity"依赖项属性将数据绑定到ViewModel中的属性.(此ViewModel是我们通过MVVMLight ViewModelLocator查看的datacontext).

这一切都很棒!绑定工作,属性设置像我期望的那样.一切都很好,直到验证.

我们使用DataAnnotations来装饰我们的ViewModel属性.ViewModel包含INotifyDataErrorInfo的自定义实现.我们为大多数输入控件实现了自定义样式,以在控件周围显示红色边框,并在控件旁边显示一条消息,显示验证错误消息.所有这些在正常情况下都很有效(例如,View上的Textbox绑定到视图模型中的属性).

当我尝试使用此用户控件的相同方法时,我最终得到的是整个用户控件周围的红色边框,并且实际文本框上没有错误指示.似乎存在错误的事实正在UI中反映出来,但它只是没有让它成为我想要的控件.

我已经在stackoverflow上搜索了这个问题,对于那些带有解决方案的问题,似乎没有一个适用于我的情况.

我的第一个猜测是,因为实际的文本框直接绑定到依赖项属性本身而不是我的视图模型上的属性,所以没有正确通知生成的错误.有没有办法通过usercontrol传播viewmodel中生成的错误,然后传递到文本框?

你能给予的任何帮助或建议都会很棒,谢谢.

这是UserControl xaml.

<UserControl x:Class="SampleProject.UserControls.SampleControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"  x:Name="sampleControl"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="LayoutRoot" DataContext="{Binding ElementName=sampleControl}">
        <TextBox Text="{Binding Path=Quantity, ValidatesOnDataErrors=True}" Width="100" Height="30" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

UserControl代码背后.

public partial class SampleControl : UserControl
{
    public SampleControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty QuantityProperty = 
         DependencyProperty.Register("Quantity", typeof(int?), typeof(SampleControl), 
    new FrameworkPropertyMetadata{DefaultValue=null, BindsTwoWayByDefault = true});

    public int? Quantity
    {
        get { return (int?)GetValue(QuantityProperty); }
        set { SetValue(QuantityProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

用于视图.

<userControls:SampleControl …
Run Code Online (Sandbox Code Playgroud)

c# validation wpf mvvm

6
推荐指数
1
解决办法
3222
查看次数

使用ValueInjecter将EntityFramework POCO复制到DTO而不触发延迟加载列表和属性

我在使用ValueInjecter创建EntityFramework POCO的深层克隆到类似的DTO类时遇到了问题.

如果我从具有导航属性的多个相关实体/子实体的复杂POCO对象注入更简单的DTO,则ValueInjecter似乎仍然触及多个属性值并导致从数据库延迟加载此数据.

我相信ValueInjecter获取特定源对象中每个属性的值,因为它准备将值注入指定的目标.

我的实际项目相当复杂,但作为一个例子,我采用了NerdDinner示例并以更简单的方式复制了该问题.(NerdDinner是使用EF4的第一个代码示例(ScottGu NerdDinner示例).

所以我有两个模型类.

public class Dinner
{
    public int DinnerId { get; set; }
    public string Title { get; set; }
    public DateTime EventDate { get; set; }
    public string Address { get; set; }
    public string HostedBy { get; set; }
    public virtual ICollection<RSVP> Rsvps { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class RSVP
{
    public int RsvpID { get; set; }
    public int DinnerID { get; set; }
    public string AttendeeEmail { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-4 valueinjecter

5
推荐指数
1
解决办法
2321
查看次数

如何使存储在Web API SessionToken中的ClaimsIdentity声明过期/更新

我使用优秀的Thinktecture.IdentityModel库在ASP.NET Web API项目中执行身份验证/授权,该项目将从移动设备和Web客户端使用.我正在使用基本身份验证来验证移动客户端以访问web api,并利用Thinktecture.IdentityModel提供的内置SessionToken生成.但是我对如何撤销添加到ClaimsIdentity声明集合中的声明有一些担忧,然后(我认为)编码到提供给客户端的SessionToken中......

这是我到目前为止:

按照IdentityModel示例项目中的示例,我创建了以下类

public static class  SecurityConfig
{
  public static void ConfigureGlobal(HttpConfiguration globalConfig)
  {
      globalConfig.MessageHandlers.Add(new AuthenticationHandler(CreateConfiguration()));
  }

  public static AuthenticationConfiguration CreateConfiguration()
  {
      var authentication = new AuthenticationConfiguration()
          {
              ClaimsAuthenticationManager = new MyClaimsTransformer(),
              RequireSsl = false, //TODO:TESTING only
              EnableSessionToken = true,
              SessionToken = new SessionTokenConfiguration()
              {
                  EndpointAddress = "/Authenticate"
              }
          };
       authentication.AddBasicAuthentication(Membership.ValidateUser);

      return authentication;
   }
}
Run Code Online (Sandbox Code Playgroud)

这是从我的Global.asax类调用的

SecurityConfig.ConfigureGlobal(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)

移动设备从个人收集用户名和密码,并正确设置身份验证标头并将凭据提供给必要的Web端点 http://myhost/api/Authenticate

在服务器上,使用用户名/密码调用Membership.ValidatUser,如果验证,MyClaimsTransformer则调用Authenticate方法.

public class ClaimsTransformer : ClaimsAuthenticationManager
{
   public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
   {
     if …
Run Code Online (Sandbox Code Playgroud)

c# claims-based-identity asp.net-web-api thinktecture-ident-model

5
推荐指数
1
解决办法
5267
查看次数