[英]WPF usercontrols in window - dependency property value is null on doubleclick
我已經在這里上傳了示例項目: https : //www.file-upload.net/download-13252079/WpfApp1.zip.html這是一個WPF窗口,其中包含多個相同的用戶控件。 UserControl具有一個名為“ Text”的依賴項屬性,該屬性綁定到MainWindowViewModel的屬性,並成功顯示在UserControl的TextBlock中。 但是,如果我雙擊UserControl並希望它提供其依賴項屬性的值,則該值為null。 為什么是這樣? 非常感謝你的幫助!
編輯:對不起,這是一些源代碼:UserControl的XAML:
<UserControl x:Class="WpfApp1.UserControl1"
...
x:Name="UC1">
<StackPanel Orientation="Vertical">
<TextBlock Margin="5" Text="Test" FontSize="20"></TextBlock>
<TextBlock Margin="5" Text="{Binding ElementName=UC1, Path=Text}" FontSize="20"></TextBlock>
</StackPanel>
</UserControl>
UserControl的代碼:
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
public UserControl1()
{
InitializeComponent();
}
string text;
public string Text
{
get { return text; }
set { SetProperty(ref text, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(UserControl1));
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
主窗口的XAML:
<Window x:Class="WpfApp1.MainWindow"
...>
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel>
<local:UserControl1 Text="{Binding Values[0]}"
MouseDoubleClick="UserControl1_MouseDoubleClick">
</local:UserControl1>
<local:UserControl1 Text="{Binding Values[1]}"
MouseDoubleClick="UserControl1_MouseDoubleClick">
</local:UserControl1>
<local:UserControl1 Text="{Binding Values[2]}"
MouseDoubleClick="UserControl1_MouseDoubleClick">
</local:UserControl1>
<local:UserControl1 Text="{Binding Values[3]}"
MouseDoubleClick="UserControl1_MouseDoubleClick">
</local:UserControl1>
</StackPanel>
</Window>
主窗口的代碼如下:
private void UserControl1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (sender is UserControl1)
{
// why is (sender as UserControl1).Text null?
MessageBox.Show("Text is: " + (sender as UserControl1).Text);
}
}
主窗口的視圖模型:
class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
Values = new ObservableCollection<string>();
Values.Add("first string");
Values.Add("second string");
Values.Add("third string");
Values.Add("fourth string");
}
#region Values
private ObservableCollection<string> values;
public ObservableCollection<string> Values
{
get { return values; }
set { SetProperty(ref values, value); }
}
#endregion
}
這是您的UserControl后面的代碼的樣子。 您不需要實現INotifyPropertyChanged。
有關所有詳細信息,請參見自定義依賴項屬性 。 具體來說,您必須從Text
屬性包裝器的getter和setter調用GetValue
和SetValue
(以及其他任何東西)。
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
nameof(Text), typeof(string), typeof(UserControl1));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
為了在自己的XAML中綁定到UserControl的Text屬性,可以使用RelativeSource
而不是ElementName來保存無用的生成的類成員:
<UserControl x:Class="WpfApp1.UserControl1" ...>
...
<TextBlock Text="{Binding Text,
RelativeSource={RelativeSource AncestorType=UserControl}}" .../>
...
</UserControl>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.