【问题标题】:Binding to several Prperties绑定到多个属性
【发布时间】:2011-04-28 04:12:48
【问题描述】:

如何在 WPF 中绑定多个属性?
我知道您可以使用ItemStringFormat="{0} my hard coded string" 来描述字符串值,但我对ItemStringFormat="{0} ({1})" 之类的东西很感兴趣,其中第0 项是属性,第1 项是绑定类中的属性。

public class ExchangeRate
{
    public int ID { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
   public decimal Rate { get; set; }
}

<ComboBox Margin="5,0" Name="Currency" ItemsSource="{Binding}" DisplayMemberPath="Description" SelectedValuePath="Code"/>

这会给我一个包含所有货币描述的列表,但我想要的是这样的

“美元 (USD)”

“美元”在哪里是属性描述和“USD”是财产代码

【问题讨论】:

    标签: .net-3.5 c#-3.0 binding


    【解决方案1】:

    您可以使用MultiBindingIMultiValueConverter 将多个来源转换为单个目标属性(即:文本)。

    【讨论】:

      【解决方案2】:

      是的,您是正确的,为了文档,我将使用上面的示例在此处提供该解决方案。

      <ComboBox Margin="5,0" Name="CurrentCurrency" ItemsSource="{Binding}">
      <ComboBox.ItemTemplate>
      <DataTemplate>
      <TextBlock DataContext="{Binding}">
      <TextBlock.Text>
      <MultiBinding StringFormat="{}{0} ({1})">
      <Binding Path="Description" />
      <Binding Path="Code" />
      </MultiBinding>
      </TextBlock.Text>
      </TextBlock>
      </DataTemplate>
      </ComboBox.ItemTemplate>
      </ComboBox>

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多