[英]Confused about Binding in UserControl
我正在創建一個UserControl對象,我正在嘗試使用Binding(使用PropertyChanged)分配值。 我創建了一個原型,當我在ViewModel中賦值時,該值不會出現或修改UserControl組件,但如果我直接在視圖中的UserControl對象中賦值,則修改有效。 我想了解我做錯了什么,因為如果我只是在我的窗口上添加一個對象並直接綁定(再次使用PropertyChanged),我的工作正常。 請遵循以下代碼。
謝謝你的幫助。
最好的祝福,
古斯塔沃。
用戶控件:
<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>
**我的Code-Behind / 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));
}
}
}
您的Window
沒有DataContext
。 綁定無法解決。
嘗試這個:
public MainWindow()
{
InitializeComponent();
DataContext = this; //This is what you're missing!
TitleVM = "açsldkfjasçldkfj";
}
編輯:
你在UserControl
也缺少同樣的東西
public UserControl1()
{
InitializeComponent();
DataContext = this;
}
在HighCore的幫助下,我發現了一個問題,另一個問題是我的UserControl沒有定義名稱。
剛剛放:
x:Name="UserControl"
進入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;
關於UserControl的代碼背后。 至少在這里只會導致bug,沒有值出現。
感謝HighCore為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.