从自定义ComboBox获取文本值

时间:2013-09-20 20:21:23

标签: wpf vb.net xaml combobox multibinding

我有一个ComboBox定义如下:

<ComboBox Width="200" Height="30" Grid.Column="0" x:Name="ExistingSpeciesComboBox" 
          ItemsSource="{Binding SpeciesColorCollection}" HorizontalAlignment="Left">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Fill="{Binding Path=Brush}" Stroke="Black" StrokeThickness="1" Height="15" Width="30"/>
                <w:WTextBlock Text="{Binding Name}" VerticalAlignment="Center" 
                              Foreground="{StaticResource SmallControlForegroundBrush}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>

SpeciesColorCollection是ColorObject

类型的ObservableCollection
Public Class ColorObject
    Public Property Name As String
    Public Property Brush As Brush
End Class

ComboBox正确显示集合中的项目,但我的问题是当我尝试从MultiBinding中的ComboBox中获取所选文本时,我收到ColorObject而不是名称。如何从ComboBox的WTextBlock中获取“Name”的值?我用于命令的绑定如下。转换器只返回字符串。

<MultiBinding Converter="{StaticResource mySpeciesSetupConverter}">
    <MultiBinding.Bindings>
        <Binding ElementName="NewSpeciesName" Path="Text" />
        <Binding ElementName="ExistingSpeciesComboBox" Path="Text" />
    </MultiBinding.Bindings>
 </MultiBinding>

1 个答案:

答案 0 :(得分:1)

<MultiBinding Converter="{StaticResource mySpeciesSetupConverter}">
    <MultiBinding.Bindings>
        <Binding ElementName="NewSpeciesName" Path="Text" />
        <Binding ElementName="ExistingSpeciesComboBox" Path="SelectedItem.Name" />
    </MultiBinding.Bindings>
</MultiBinding>