我可以使用多重绑定进行文本搜索

Rel*_*ity 2 wpf text mvvm multibinding

我在mvvm-wpf应用程序中有下面的组合框.我需要在此实现"文本搜索"..(以及多重绑定).请有人帮帮我.

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Bid Service Cat ID"
                Margin="2"></TextBlock>
    <ComboBox Width="200"
                Height="20"
                SelectedValuePath="BidServiceCategoryId"
                SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                    Path=DataContext.SelectedBidServiceCategoryId.Value}"
                ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                    Path=DataContext.BenefitCategoryList}"
                Margin="12,0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock DataContext="{Binding}">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0}: {1}">
                                <Binding Path="BidServiceCategoryId" />
                                <Binding Path="BidServiceCategoryName" />
                            </MultiBinding>
                            </TextBlock.Text></TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 6

不幸的是,TextSearch.Text在DataTemplate中不起作用.否则你可能会做这样的事情

<ComboBox ...>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="TextSearch.Text">
                <Setter.Value>
                    <MultiBinding StringFormat="{}{0}: {1}">
                        <Binding Path="BidServiceCategoryId"/>
                        <Binding Path="BidServiceCategoryName"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,所以我看到了两个问题的解决方案.

第一种方式
您可以设置IsTextSearchEnabledTrueComboBox,覆盖ToString在你的源类和改变MultiBindingTextBlock一个Binding

XAML

<ComboBox ...
          IsTextSearchEnabled="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

来源类

public class TheNameOfYourSourceClass
{
    public override string ToString()
    {
        return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName);
    }
    //...
}
Run Code Online (Sandbox Code Playgroud)

第二种方法
如果您不希望覆盖的ToString我认为你必须在你把你的源类引入一个新的属性BidServiceCategoryId,并BidServiceCategoryNameTextSearch.TextPath.在这个例子中,我称之为BidServiceCategory.为此,您必须在OnPropertyChanged("BidServiceCategory");何时BidServiceCategoryIdBidServiceCategoryName更改时进行呼叫.如果它们是正常的CLR属性,则可以执行此操作set,如果它们是依赖项属性,则必须使用属性更改的回调

XAML

<ComboBox ...
          TextSearch.TextPath="BidServiceCategory"
          IsTextSearchEnabled="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock DataContext="{Binding}">
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0}: {1}">
                        <Binding Path="BidServiceCategoryId" />
                        <Binding Path="BidServiceCategoryName" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

来源类

public class TheNameOfYourSourceClass
{
    public string BidServiceCategory
    {
        get
        {
            return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName);
        }
    }

    private string m_bidServiceCategoryId;
    public string BidServiceCategoryId
    {
        get
        {
            return m_bidServiceCategoryId;
        }
        set
        {
            m_bidServiceCategoryId = value;
            OnPropertyChanged("BidServiceCategoryId");
            OnPropertyChanged("BidServiceCategory");
        }
    }

    private string m_bidServiceCategoryName;
    public string BidServiceCategoryName
    {
        get
        {
            return m_bidServiceCategoryName;
        }
        set
        {
            m_bidServiceCategoryName = value;
            OnPropertyChanged("BidServiceCategoryName");
            OnPropertyChanged("BidServiceCategory");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)