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));
}
}
}
답장을 보내 주셔서 감사합니다. 나는 그것을 추가했고 값이 올 것보다 중단 점을 볼 수 있지만 표시되는 것은 'açsldkfjasçldkfj'가 아닌 'MainWindow'텍스트뿐입니다. 모든 단서? –
@ GustavoGonçalves 내 편집을 참조하십시오. –
나는 그것도했다. 하지만 그렇게하면 UserControl1.xaml 내부에 중단 점이 생깁니다.cs, TitleProperty에있는 것을 제외하면 MainWindow에서 값을 얻지 못합니다. –