我的WPF应用程序的DataContext包含一个IVariableConverter对象,该对象与DataContext交互,并且不是静态的
// Simplified Example
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
DataContext = viewModel;
}
}
//I am using Fody PropertyChanged
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
public int someIndex { get; set;}
// The converter that I want to use, which implements the IValueConverter interface
public SomeConverter someConverter { get; set; }
public ViewModel()
{
// ... setting the other variables ...
someConverter = new SomeConverter(this, ...);
}
}
现在我有一个TextBlock,它使用到'someIndex'变量的绑定,我想使用来自DataContext(它是'ViewModel'的对象)的'someConverter'进行转换
我最好的猜测是只使用转换器的名称,就像绑定一样,但是不幸的是这没有帮助
<TextBlock Text="{binding someIndex, Converter=someConverter}"/>
我如何访问转换器?
感谢您的帮助!