ComboBox总是填充System.Data.DataRowView

时间:2018-02-13 17:58:12

标签: c# wpf combobox

下面的代码用于填充ComboBox下拉列表中的公司名称及其地址,问题是,一旦我实际从下拉列表中选择一个值,它总是只用“System.Data.DataRowView”填充文本框< / p>

任何人都知道如何解决这个问题?

Current Implementation

<StackPanel Orientation="Horizontal" Margin="0,10,0,0" DataContext="{Binding parties}">
    <TextBlock Text="Supplier" Style="{StaticResource tabTextBlock}"/>
    <ComboBox ItemsSource="{Binding Vwr.Table.Tbl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      x:Name="Supplier"
      SelectedIndex="{Binding Vwr.Table.GridSelIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      SelectedItem="{Binding Vwr.Table.Vals[1].Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      IsEditable="True" 

            Style="{StaticResource tabTextBox}"
      >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{} {0} , {1}">
                            <Binding Path="Name"/>
                            <Binding Path="Address"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>

    </ComboBox>
</StackPanel>

1 个答案:

答案 0 :(得分:1)

您可以将TextSearch.TextPath属性设置为DataTable中列的名称:

<ComboBox x:Name="Supplier" TextSearch.TextPath="Name" ... />

如果您要同时显示所选行的NameAddress,则应向DataTable添加一列:

dataTable.Columns.Add(new DataColumn("Combined", typeof(string), "Name + ', ' + Address"));

...并将DisplayMemberPath属性设置为此列的名称:

<ComboBox ItemsSource="{Binding Vwr.Table.Tbl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      x:Name="Supplier"
                      SelectedIndex="{Binding Vwr.Table.GridSelIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      SelectedItem="{Binding Vwr.Table.Vals[1].Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      IsEditable="True" 
                      Style="{StaticResource tabTextBox}"
                      DisplayMemberPath="Combined" TextSearch.TextPath="Combined">
</ComboBox>

使用ItemTemplate不是解决方案。