I have a ListView
where I display a collection of entities.
One of these columns contains an abbreviation, and I would like to display another text. I have both the abbreviation and the 'display'-string in a dictionary.
Is there any way I can display the 'display'-string from the dictionary instead of the abbreviation, without changing the entities?
You could use a value Converter
, eg:
View
<ListView.View>
<GridView>
<GridViewColumn Header="Abbreviation">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Abv" Converter="{StaticResource abvConverter}"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
</GridView>
</ListView.View>
Value Converter
[ValueConversion(typeof(string), typeof(string))]
public class AbbreviationConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// error checking
return myDictionaryInstance[value.ToString()];
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
...
}
}
If you have declared the dictionary like this in the view model private Dictionary m_DictRows;
public Dictionary<string, string> DictRows
{
get { return m_DictRows; }
set { m_DictRows = value; }
}
then in the xaml just need to bind in the below manner:
<ListView ItemsSource="{Binding DictRows, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" Style="{DynamicResource txtID}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.