我试图绑定到用户控件自定义类型的属性(在我们的例子中,我们称之为DataContextOne
)。此自定义类型由两个字符串组成。
然后我有一个用户控件,我想绑定这个自定义类型。
奇怪的是,如果我输入2个字符串属性,然后尝试从我的自定义类型绑定每个字符串,它就可以了。但是当我在自定义控件中创建一个DataContextOne
属性并尝试绑定它时,没有任何反应(在用户控件中为null)。
这是我的代码
public class DataContextOne : INotifyPropertyChanged
{
protected virtual void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private string _title;
public string Title
{
get { return _title; }
set
{
if (_title != value)
{
_title = value;
TitleModified = _title + " modified";
RaisePropertyChanged("Title");
}
}
}
private string _titlemodified;
public string TitleModified
{
get { return _titlemodified; }
set
{
if (_titlemodified != value)
{
_titlemodified = value;
RaisePropertyChanged("TitleModified");
}
}
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
(Content as FrameworkElement).DataContext = this;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(MyUserControl),
null);
public string TextModified
{
get { return (string)GetValue(TextModifiedProperty); }
set { SetValue(TextModifiedProperty, value); }
}
public static readonly DependencyProperty TextModifiedProperty = DependencyProperty.Register(
"TextModified",
typeof(string),
typeof(MyUserControl),
null);
}
背后的代码
public partial class MainWindow : Window
{
Random Rnd = new Random();
DataContextOne dtone = new DataContextOne();
public MainWindow()
{
InitializeComponent();
dtone.Title = Rnd.Next().ToString();
DataContext = dtone;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dtone.Title = Rnd.Next().ToString();
}
}
XAML:
<local:MyUserControl Grid.Row="1" Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" TextModified="{Binding TitleModified, UpdateSourceTrigger=PropertyChanged}"/>
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
(Content as FrameworkElement).DataContext = this;
}
public DataContextOne dtone
{
get { return (DataContextOne)GetValue(dtoneProperty); }
set { SetValue(dtoneProperty, value); }
}
public static readonly DependencyProperty dtoneProperty = DependencyProperty.Register(
"dtone",
typeof(DataContextOne),
typeof(MyUserControl),
null);
}
Code Behind(请注意我绑定this
)
public partial class MainWindow : Window
{
Random Rnd = new Random();
DataContextOne dtone = new DataContextOne();
public MainWindow()
{
InitializeComponent();
dtone.Title = Rnd.Next().ToString();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dtone.Title = Rnd.Next().ToString();
}
}
XAML
<local:MyUserControl Grid.Row="1" dtone="{Binding dtone, UpdateSourceTrigger=PropertyChanged}" Margin="0,31,200,169"/>
我不明白缺少什么。对我来说,只有一个自定义类型封装第二个版本的字符串是一样的,所以为什么它不起作用?
答案 0 :(得分:1)
{Binding dtone}
尝试绑定dtone
DataContext
的{{1}}属性,即MainWindow
。 dtone
类中没有MainWindow
属性,只有私有字段,您无法绑定到字段。
可能的解决方案:
dtone
属性DataContext = this
更改为DataContext = dtone
和{Binding dtone}
更改为{Binding}
<强>更新强>
可能不相关,但似乎没有理由在dtone
中拥有MyUserControl
属性。为什么不简单
<local:MyUserControl Grid.Row="1" DataContext="{Binding dtone}" />
而不是尝试在DataContext
构造函数中手动设置MyUserControl
?