我有以下轉換器:用x綁定到與轉換器電流的DataContext:綁定
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
Debug.WriteLine(value.GetType());
//The rest of the code
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
並試圖使用轉換器的XAML:
<ListView ItemsSource="{x:Bind StickersCVS.View}" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:StickerCategory">
<TextBlock Foreground="{x:Bind Converter={StaticResource MyConverter}}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
這給了我一個NPE在value.GetType()
,顯然通過的價值是null
。
如果我改變了以下部分:
<TextBlock Foreground="{x:Bind Converter={StaticResource MyConverter}}"/>
到
<TextBlock Foreground="{Binding Converter={StaticResource MyConverter}}"/>
然後,它的工作原理。 Debug
正確輸出StickerCategory
作爲值的類型。任何原因爲什麼x:Bind
通過null
到轉換器,我如何使它與x:Bind
一起工作?我試圖將DataContext
傳遞給我的轉換器。
注意,x的默認模式:綁定是一次性(綁定默認爲單向)。這可能是根本原因嗎?也許這個值在第一個綁定時真的是空的...... – gregkalapos
不,沒有骰子:(我試着指定綁定模式: ' ' 仍NPE。無論如何感謝:) –
你真的確定嗎?無論是否使用「Mode = OneWay」,它都會首先爲空 - 但如果沒有它,它永遠不會改變。所以既然你沒有在你的轉換器中有一個空檢查,你的程序可能永遠不會出現這樣的情況,即實際上存在一個值?! –