2013-03-08 48 views
0

我創建一個用戶控件對象,我試圖把使用綁定(帶的PropertyChanged)值。我做了一個原型,當我在ViewModel中賦值時,該值不會出現或者修改UserControl組件,但是如果我直接在視圖中的UserControl對象中賦值,那麼修改就可以工作。我想明白我做錯了什麼,因爲工作正常,如果我只是在我的窗口添加一個對象,並直接綁定(再次,使用PropertyChanged)。 請按照下面的代碼。困惑綁定在用戶控件

感謝您的任何幫助。

最好的問候,

Gustavo。

用戶控件:

<UserControl x:Class="WpfControlLibrary1.UserControl1" 
     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" 
     d:DesignHeight="30" d:DesignWidth="300"> 
<Grid> 
    <TextBlock Text="{Binding Title}" /> 
</Grid> 
</UserControl> 

代碼用戶控制的背後:

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

    #region Title Property 

    public static String GetTitle(DependencyObject obj) 
    { 
     return (String)obj.GetValue(TitleProperty); 
    } 

    public static void SetTitle(DependencyObject obj, String value) 
    { 
     obj.SetValue(TitleProperty, value); 
    } 

    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.RegisterAttached(
      "Title", 
      typeof(String), 
      typeof(UserControl1), 
      new FrameworkPropertyMetadata(TitleChangedCallback) 
      ); 

    private static void TitleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     UserControl1 _this = (d as UserControl1); 
    } 

    private String title; 
    public String Title 
    { 
     get { return title; } 
     set 
     { 
      title = value; 
      OnPropertyChanged("Title"); 
     } 
    } 

    #endregion 

    #region INotifyPropertyChanged event and method 

    public event PropertyChangedEventHandler PropertyChanged; 

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

    #endregion 

**我的窗口:**

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:uc="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <uc:UserControl1 Title="{Binding TitleVM, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
</Grid> 
</Window> 

**我的代碼隱藏/ ViewModel:**

public partial class MainWindow : INotifyPropertyChanged 
{ 
    // Notify WPF that Counter changed 
    public event PropertyChangedEventHandler PropertyChanged; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     TitleVM = "açsldkfjasçldkfj"; 
    } 

    private String titleVM; 
    public String TitleVM 
    { 
     get { return titleVM; } 
     set 
     { 
      titleVM = value; 
      OnPropertyChanged("TitleVM"); 
     } 
    } 

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

} 

回答

0

同樣的事情HighCore的幫助下,我找到了一個問題,另一個問題是我的UserControl沒有定義名稱。

只要把:

x:Name="UserControl" 

進入用戶控件的XAML,並將努力。此外,我已將我的代碼修改爲此:

 public String Title 
    { 
     get { return (String)base.GetValue(TitleProperty); } 
     set { base.SetValue(TitleProperty, value); } 
    } 

    public static readonly DependencyProperty TitleProperty = 
    DependencyProperty.RegisterAttached(
     "Title", 
     typeof(String), 
     typeof(UserControl1), 
     new FrameworkPropertyMetadata(null) 
     ); 

乾淨的代碼給我們的眼睛。

PS:有沒有必要把:

DataContext = this; 

開啓代碼用戶控件之後。至少在這裏只會導致bug,沒有任何價值。

感謝HighCore的幫助。

2

您的Window沒有DataContext。綁定無法解析。

試試這個:

public MainWindow() 
{ 
    InitializeComponent(); 

    DataContext = this; //This is what you're missing! 

    TitleVM = "açsldkfjasçldkfj"; 
} 

編輯:

你也缺少在UserControl

public UserControl1() 
{ 
    InitializeComponent(); 
    DataContext = this; 
} 
+0

感謝您的回答。我補充說,我可以用斷點看到值比即將到來,但顯示的只是'MainWindow'文本,而不是'açsldkfjasçldkfj'。任何線索? – 2013-03-11 12:24:05

+1

@GustavoGonçalves看到我的編輯。 – 2013-03-11 12:27:25

+0

我也是。但是當我這樣做時,我在UserControl1.xaml中的斷點。cs,除了TitleProperty中的一個沒有進入,他們沒有從MainWindow獲得任何值。 – 2013-03-11 12:52:04