2013-03-08 3 views
0

UserControl 개체를 만들고 있는데 Binding (PropertyChanged 포함)을 사용하여 값을 할당하려고합니다. 프로토 타입을 만들 때 값을 ViewModel에 할당 할 때 값이 나타나지 않거나 UserControl 구성 요소를 수정하지만 뷰에있는 UserControl 개체에서 직접 값을 할당하면 수정이 작동합니다. 내가 잘못하고있는 일을 이해하고 싶습니다. 왜냐하면 저는 단지 창에 객체를 추가하고 직접 PropertyChanged를 사용하여 바인딩하면 작동합니다. 아래 코드를 따르십시오.UserControl에서 바인딩에 대해 혼동했습니다.

도움 주셔서 감사합니다.

최고 감사합니다,

구스타보.

UserControl을 : 사용자 컨트롤의 뒤에

<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

에서 같은 일을 놓치고있어, 내가 한 가지 문제를 발견했습니다, 다른 문제는 내 UserControl에 정의 된 이름이 없다는 것입니다. UserControl을의 XAML 속으로

x:Name="UserControl" 

및 작동합니다

그냥 넣어. 또한 내 코드를 다음과 같이 수정했습니다.

 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 : 넣을 필요가 없습니다 : 해당 UserControl의 뒤에 코드에

DataContext = this; 

. 적어도 버그에 대해서만 결과가 나오고 아무 가치도 없었습니다.

고맙습니다. 도움을 요청하십시오.

2

귀하의 Window에는 DataContext이 없습니다. 바인딩을 해결할 수 없습니다.

이 시도 :

public MainWindow() 
{ 
    InitializeComponent(); 

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

    TitleVM = "açsldkfjasçldkfj"; 
} 

편집 :

또한 HighCore의 도움으로 UserControl

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

답장을 보내 주셔서 감사합니다. 나는 그것을 추가했고 값이 올 것보다 중단 점을 볼 수 있지만 표시되는 것은 'açsldkfjasçldkfj'가 아닌 'MainWindow'텍스트뿐입니다. 모든 단서? –

+1

@ GustavoGonçalves 내 편집을 참조하십시오. –

+0

나는 그것도했다. 하지만 그렇게하면 UserControl1.xaml 내부에 중단 점이 생깁니다.cs, TitleProperty에있는 것을 제외하면 MainWindow에서 값을 얻지 못합니다. –

관련 문제