wpf 如何在没有绑定路径的情况下使用转换器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14524373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:13:46  来源:igfitidea点击:

How to use converters without binding paths?

wpfxamlbindingcombobox

提问by Eduardo Brites

I have a Combobox whose ItemsSource is an ObservableCollection of intvalues. My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.

我有一个组合框,它的 ItemsSource 是一个int值的ObservableCollection。我的组合框 itemtemplate 包含一个图像和一个文本块,其内容由 2 个转换器提供。

How can I set this 2 bindings? The following code does not compile:

如何设置这两个绑定?以下代码无法编译:

<ComboBox.ItemTemplate>
     <DataTemplate>
          <StackPanel Orientation="Horizontal">
               <Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
               <TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
          </StackPanel>
     </DataTemplate>
</ComboBox.ItemTemplate>

回答by CodeNaked

You need to remove the ,so:

你需要删除,这样:

<ComboBox.ItemTemplate>
     <DataTemplate>
          <StackPanel Orientation="Horizontal">
               <Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
               <TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
          </StackPanel>
     </DataTemplate>
</ComboBox.ItemTemplate>