【问题标题】:Get text value from custom ComboBox从自定义 ComboBox 获取文本值
【发布时间】:2013-09-26 08:10:30
【问题描述】:

我有一个 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 中获取“名称”的值?我用于命令的绑定如下。转换器只返回字符串。

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

【问题讨论】:

    标签: wpf vb.net xaml combobox multibinding


    【解决方案1】:
    <MultiBinding Converter="{StaticResource mySpeciesSetupConverter}">
        <MultiBinding.Bindings>
            <Binding ElementName="NewSpeciesName" Path="Text" />
            <Binding ElementName="ExistingSpeciesComboBox" Path="SelectedItem.Name" />
        </MultiBinding.Bindings>
    </MultiBinding>
    

    【讨论】:

    • 谢谢,成功了。我知道它必须是一些简单的东西,但我就是不能把我的手指放在它身上!
    • @dkirkdrei 太好了,所以你为什么不接受答案 ;)
    • 回答已接受!我正在尝试,但它让我等了 7 分钟??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多