WPF列表框上的多重绑定



如何在列表框上使用这样的代码??:

<TextBlock>
    <TextBlock.Text>    
        <MultiBinding StringFormat="{}{0} + {1}">
            <Binding Path="Name" />
            <Binding Path="ID" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

我想在列表框上执行上述操作。我的数据源有两个项目,名字和姓氏,我想显示这两个。我试过这个,但它不工作:

<ListBox ItemsSource="{Binding}" Name="listBox" Width="200" DockPanel.Dock="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged="selectionChanged" >
                <ListBox.DisplayMemberPath>
                    <MultiBinding StringFormat="{}{0} + {1}">
                        <Binding Path="forenames" />
                        <Binding Path="surname" />
                    </MultiBinding>
                </ListBox.DisplayMemberPath>
            </ListBox>

请注意,我想使用像上面一样的xaml,而不是一个单独的IConverter类或其他东西

用自定义的ItemTemplate代替DisplayMemberPath

<ListBox ItemsSource="{Binding}" Name="listBox" Width="200" DockPanel.Dock="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged="selectionChanged" >
   <ListBox.ItemTemplate>
      <DataTemplate>
         <TextBlock>
            <TextBlock.Text>
               <MultiBinding StringFormat="{}{0} + {1}">
                  <Binding Path="forenames" />
                  <Binding Path="surname" />
               </MultiBinding>
            </TextBlock.Text>
         </TextBlock>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

相关内容

  • 没有找到相关文章

最新更新