[英]Silverlight on Windows Phone: IValueConverter is never called for a custom bound property
我有一個用戶控件,其屬性定義如下:
public partial class ChartEx : UserControl
{
private object _dataSource;
public object DataSource
{
get { return _dataSource; }
set
{
_dataSource = value; //break here
}
}
}
在我的xaml中,我試圖將它綁定到一個集合(來自內部數據模板):
<DataTemplate x:Key="tmplCounter">
<my:ChartEx
DataContext="{Binding Converter={StaticResource convTest}, ConverterParameter='DataContext'}"
DataSource="{Binding Converter={StaticResource convTest}, ConverterParameter='DataSource'}">
</my:ChartEx>
</DataTemplate>
但是在調試器中我看到DataSource setter的值是System.Windows.Data.Binding類型的!
永遠不會使用參數“DataSource”輸入值轉換器,但對於“DataContext”,它可以完美地工作。 轉換器僅用於調試目的,沒有它的工作方式相同。
如何為我定義的屬性進行綁定工作?
好的,對不起,我是個白痴。 事實證明,在XAML中,您無法綁定到“簡單”屬性。 prop必須是依賴屬性。 以下作品:
public partial class ChartEx : UserControl
{
public event EventHandler DataSourceChanged;
public object DataSource
{
get { return GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register(
"DataSource",
typeof(object),
typeof(ChartEx),
new PropertyMetadata(true, OnDataSourcePropertyChanged));
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ChartEx source = d as ChartEx;
if (source.DataSourceChanged != null)
source.DataSourceChanged(source, new EventArgs());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.