WPF组合框自定义控件文本搜索功能

时间:2014-11-10 12:44:37

标签: c# wpf mvvm wpf-controls

我在WPF MVVM应用程序中有一个cutom组合框控件,我正在尝试在框中启用serach。这意味着只要用户在cmb框中键入任何内容,它就会显示那些相关记录。

我正在使用下面的代码,但无法看到这个工作。

<ComboBox x:Class="ABC.Selector"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:ABC.ViewModels"
        TextSearch.TextPath ="{Binding Name}" IsEditable="True">
 >
 <ComboBox.ItemTemplate>
    <DataTemplate DataType="{x:Type vm:MyViewModel}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Image}" Margin="0 0 5 0"/>
            <TextBlock Text="{Binding Name}"/>
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

此功能适用于以下代码:

    <ComboBox Grid.Column="3" Grid.Row="1" HorizontalAlignment="Stretch" Name="cmb" 
              VerticalAlignment="Stretch" Height="Auto" Grid.ColumnSpan="9"
              ItemsSource="{Binding Details}"
              SelectedItem="{Binding Selected, Mode=TwoWay}">
        <TextSearch.TextPath>Name</TextSearch.TextPath>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} - {1}">
                                <Binding Path="Name" />
                                <Binding Path="LongName" />
                            </MultiBinding>
                        </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

知道如何在第一个代码块中启用它吗?

2 个答案:

答案 0 :(得分:0)

TextPath不期望绑定到实际值;它希望搜索属性的名称。您应该设置TextSearch.TextPath="Name"

答案 1 :(得分:0)

public String executeCommand(String command) {
    String output = "";

        InputStream inputStream = Runtime.getRuntime().exec(command).getInputStream();

        while( inputStream.available() <= 0) 
            try { Thread.sleep(500); } catch(Exception ex) {}

        java.util.Scanner s = new java.util.Scanner(inputStream);
        while(s.hasNext())
            output += s.nextLine() + "\n";

            return output; 
}