Friday, March 31, 2023

Kind of Binding a ConverterParameter

You cannot bind the parameter of a converter because it is not a DependencyProperty. So this throws an error at run time (in InitializeComponent).

<TextBlock Text="{Binding DistrictID, Converter={StaticResource ID2DescriptionConverter}, ConverterParameter={Binding DistrictTable}}"/>



But you can use a StaticResource and a StaticResource can be bound.

Add this to your resources

<CollectionViewSource x:Key="DistrictTable" Source="{Binding DistrictTable}"/>

and change the TextBlock XAML to

<TextBlock Text="{Binding DistrictID, Converter={StaticResource ID2DescriptionConverter}, ConverterParameter={StaticResource DistrictTable}}"/>

and it works! What gets passed in as the ConverterParameter is a CollectionViewSource which has a Source property that your converter can cast to a DataTable and perform the conversion. So it's not exactly the same as binding to the DataTable, but it's pretty close.

No comments:

Post a Comment