【发布时间】:2011-11-03 08:37:38
【问题描述】:
我创建了一个 UserControl,它是根据绑定的 ObservableCollection 的内容显示转换后的字符串值。应用程序加载时一切正常;我的 IValueConverter 被调用并产生正确的字符串结果,该结果在我的 UserControl 中正确显示。但是,如果 ObservableCollection 内容发生更改,我的控件不会更新。
另外,在创建此控件之前,我有相同的行为,但绑定了常规 Button 控件的 Content 属性,这也正常工作并按预期更新。
任何想法我想用我的 UserControl 获得同样的东西吗?
控件属性的样子;
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl));
public string Text
{
get { return GetValue(TextProperty) as string; }
set { SetValue(TextProperty, value);
}
UserControl XAML(显示转换后的字符串值)中的相关部分是;
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MyUserControl}}, Path=Text}" />
并且控件像这样在单独的窗口中创建;
<CoreControls:MyUserControl
Name="myControl"
Text="{Binding Path=ObservableCollectionInstance, Converter={StaticResource MyValueConverter}, Mode=OneWay}" />
【问题讨论】:
标签: wpf data-binding user-controls dependency-properties