0
我有Languages
枚舉。而我Model.Translations
代表着不同語言的翻譯:枚舉與轉換器綁定
public ObservableCollection<LanguageValue> Translations { get; set; }
public class LanguageValue
{
public Language Key { get; set; }
public string Value { get; set; }
}
我想我的觀點有標籤 - 文本框列表中的每個翻譯項目。 但在標籤我想有類似"Caption ({0})"
,其中參數是語言名稱(枚舉爲字符串表示形式)。本文本身來自Resources。
喜歡的東西:
<ItemsControl ItemsSource="{Binding Path=Translations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="5,2,5,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="ItemLabel" VerticalAlignment="Center"
Text="{Binding Path=Key, Converter=languageConverter, ConverterParameter={x:Static res:Resources.lblCaption}}" />
<TextBox Grid.Column="1" x:Name="ItemText" VerticalAlignment="Center"
Text="{Binding Path=Value, Mode=TwoWay}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我LanguageConverter:
[ValueConversion(typeof(Language), typeof(string))]
public class LanguageToDisplayConverter : IValueConverter
{
public object Convert(object value, Type t, object parameter, CultureInfo culture)
{
return string.Format(parameter.ToString(), ((Language)value).ToString());
}
public object ConvertBack(object value, Type t, object parameter, CultureInfo culture)
{
//I guess I don't need this anyway?
return null;
}
}
但我有錯誤:
The TypeConverter for "IValueConverter" does not support converting from a string.
有什麼不對?