2012-02-29 72 views
0

我創建了一個用戶控件以在應用程序的多個位置使用它,控件有兩個屬性,女巫值綁定到視圖模型。問題是當應用程序正在加載它時拋出一個異常,同時設置用戶控件的屬性之一,任何想法?錯誤綁定用戶控件的屬性wp7

用戶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

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)); 
    } 
} 

的用途:

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

視圖模型屬性片段是一樣的顯示,該用戶控件.xaml.cs

異常被拋出

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) 

我發現了什麼是異常的起源是MainPage執行的約束力,但並沒有真正明白,爲什麼還是什麼原因造成的。

感謝

+1

'this.DataContext =這一點;'那是什麼?請從構造函數 – Ku6opr 2012-02-29 15:55:30

+0

@ Ku6opr中移除'LayoutRoot.DataContext = this;'和'this.DataContext = this;' - 你在我面前!肯定「聞到」錯誤 – Stuart 2012-02-29 15:58:19

+0

編輯代碼但問題仍然相同XD – DVD 2012-02-29 16:07:29

回答

1

你失去的DataContext通過明確設置它在你的用戶控件中。另外,您應該使用DependencyProperty。最後,您的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> 

+0

我已經注意到了,對於這個迴應很多;)。順便說一句,你的網格建議是真棒xDD – DVD 2012-02-29 21:59:55

0

我不知道,但究竟是在構造函數的代碼塊:

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

它看起來「危險」 ......

+0

不是問題,我的原始代碼現在已更新。即使在加載的事件上設置了上下文,問題仍然存在。 – DVD 2012-02-29 16:05:05

+0

我想你的viewmodel/data上下文實際上爲CurrentUser提供了一個ICredentials? – Stuart 2012-02-29 16:07:22

+0

是的,如果我把usercontrol xaml代碼放入主頁面,它一切正常,所以沒有問題。 – DVD 2012-02-29 16:13:20