WPF Combobox TextSearch使用

使用TextPath:
   <ComboBox Name="cob" IsEditable="True" StaysOpenOnEdit="True" TextSearch.TextPath="Content.Children[0].Text" >
                <ComboBoxItem>
                    <StackPanel >
                        <TextBlock Text="1"/>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem>
                    <StackPanel>
                        <TextBlock Text="2"/>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem>
                    <StackPanel >
                        <TextBlock Text="3"/>
                    </StackPanel>
                </ComboBoxItem>
            </ComboBox>

由于ComboboxItem的Content是StackPanel所以TextSearch.TextPath=Content.Children[0].Text,如果把ComboboxItem去掉则变成了

   <ComboBox Name="cob" IsEditable="True" StaysOpenOnEdit="True" TextSearch.TextPath="Children[0].Text" >
                    <StackPanel >
                        <TextBlock Text="1"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="2"/>
                    </StackPanel>
                    <StackPanel >
                        <TextBlock Text="3"/>
                    </StackPanel>
                
            </ComboBox>

如果Item的结构变了,使用TextPath就不适用了,可以使用Text来解决:

    <ComboBox Name="cob" IsEditable="True" StaysOpenOnEdit="True"   >
                <ComboBoxItem TextSearch.Text="1">
                    <StackPanel >
                        <TextBlock Text="1"/>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem TextSearch.Text="2">
                    <StackPanel>
                        <TextBlock Text="2"/>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem TextSearch.Text="3">
                    <StackPanel >
                        <TextBlock Text="3"/>
                    </StackPanel>
                </ComboBoxItem>
            </ComboBox>



你可能感兴趣的:(WPF)