简体   繁体   English

错误绑定用户控件wp7的属性

[英]Error Binding a property of a user control wp7

I created an user control to use it in multiple places around the app, the control have two properties, witch value are binded to the viewmodel. 我创建了一个用户控件以在应用程序周围的多个地方使用它,该控件具有两个属性,将女巫值绑定到viewmodel。 the problem is when the app is loading it throws an exception while setting one of the properties of the user control, any idea? 问题是当应用程序正在加载时,它会在设置用户控件的属性之一时引发异常,您知道吗?

User Control.xaml 用户控件

<UserControl x:Class="Client.Controls.CredentialsUserControl"
    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"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid Name="LayoutRoot">

        <TextBlock Text="{Binding Title}" Margin="12,20,12,390" TextWrapping="Wrap" FontSize="30"/>

        <TextBlock Text="Username" Margin="39,88,260,362" FontSize="25"/>
        <TextBlock Text="{Binding Credentials.User}" Margin="361,88,0,362" FontSize="25" />

        <TextBlock Text="Password" Margin="39,148,260,302" FontSize="25"/>
        <TextBlock Text="{Binding Credentials.Password}" Margin="361,148,0,302" FontSize="25" />

</UserControl>

UserControl.xaml.cs UserControl.xaml.cs

public partial class CredentialUserControl: UserControl , INotifyPropertyChanged 
{

    public const string CredentialsPropertyName = "Credentials";

    private ICredentials _credentials= null;
    public ICredentials Credentials
    {
        get
        {
            _credentials_report;
        }

        set
        {
            if (_credentials== value)
            {
                return;
            }

            _credentials= value;
            NotifyPropertyChanged(CredentialsPropertyName );
        }
    }

    public string Title { get; set; }



    public MobfoxReportUserControl()
    {
        InitializeComponent();

        Loaded += PageLoaded;
    }

    void PageLoaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;

    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string prop)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

The Usage: 用法:

<Controls:CredentialsUserControl  Title="Your Credentials" Credentials="{Binding CurrentUser}"/>

The ViewModel Property snippet is the same as the showed in the UserControl.xaml.cs ViewModel属性片段与UserControl.xaml.cs中显示的相同

The exception being thrown 抛出异常

System.Windows.Markup.XamlParseException occurred
  Message=Set property 'CredentialsUserControl.Credentials' threw an exception. [Line: 29 Position: 85]
  InnerException: System.ArgumentException
       Message=ArgumentException
       StackTrace:
            at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
            at System.Reflection.RuntimePropertyInfo.InternalSetValue(PropertyInfo thisProperty, Object obj, Object value, Object[] index, StackCrawlMark& stackMark)
            at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)

What I found out is that the origin of the exception is the binding on the MainPage, but didn't really understand why or what is causing it. 我发现异常的根源是MainPage上的绑定,但并没有真正理解引起该异常的原因或原因。

Thanks 谢谢

You're losing the DataContext by setting it explicitly inside your usercontrol. 通过在用户控件内显式设置DataContext ,您将丢失它。 In addition, you should use a DependencyProperty . 另外,您应该使用DependencyProperty Finally, your XAML is loaded with Exact margins... you might want to switch to use Grid Row/Column definitions, as if you ever need to change the page, it will be easier. 最后,您的XAML加载了精确的边距...您可能希望切换为使用“网格行/列”定义,就像您需要更改页面一样,这将变得更加容易。

using System.Net;
using System.Windows;
using System.Windows.Controls;

namespace Client.Controls
{
    public partial class CredentialsUserControl : UserControl
    {
        public CredentialsUserControl()
        {
            InitializeComponent();

            if (System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                Credentials = new NetworkCredential("user","pass");
                Title = "testing creds";
            }
        }



        public ICredentials Credentials
        {
            get { return (ICredentials)GetValue(CredentialsProperty); }
            set { SetValue(CredentialsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Credentials.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CredentialsProperty =
            DependencyProperty.Register("Credentials", typeof(ICredentials), typeof(CredentialsUserControl),new PropertyMetadata(null));

        public string Title { get; set; }

    }
}

<Grid Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Text="{Binding ElementName=control, Path=Title}" TextWrapping="Wrap" FontSize="30" Margin="12,24" />

    <Grid Grid.Row="1" Margin="40,0,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="12" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Text="Username" FontSize="25" Grid.Column="0" />
        <TextBlock Text="{Binding ElementName=control, Path=Credentials.User}" FontSize="25" Grid.Column="1" HorizontalAlignment="Right"/>

        <TextBlock Text="Password" Grid.Row="2" FontSize="25" Grid.Column="0" />
        <TextBlock Text="{Binding ElementName=control, Path=Credentials.Password}" Grid.Row="2" FontSize="25" Grid.Column="1" HorizontalAlignment="Right"/>
    </Grid>
</Grid>

I'm not sure, but what is this block of code in your constructor: 我不确定,但是构造函数中的这段代码是什么:

    LayoutRoot.DataContext = this;
    this.DataContext = this;

It looks "dangerous"... 看起来“危险” ...

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

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