我正在处理的模板化控件使用 ValueConverter,如下所示:
<ListBox>
<ListBox.Resources>
<Controls:CodeDescriptionValueConverter x:Key="CodeDescriptionValueConverter"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource CodeDescriptionValueConverter}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我在 generic.xaml 中提供的默认外观。当我使用这个控件时,我想将不同的格式字符串传递给转换器。有没有办法在不提供完整的 ControlTemplate 的情况下实现这一点?
我的第一个想法是我可以将带有 TemplateBinding 的 ConverterParameter 用于控件上的属性,但我发现 ConverterParameters 无法绑定。另一种选择可能是从 ConvertTo 方法访问控件,然后选择该属性。我不知道该怎么做。任何可以消除每次我使用控件时完全重新模板化控件的需要的选项都会有所帮助(这是很多 Xaml)。