質問

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>
役に立ちましたか?

解決

不運にも、 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>

ただし、これは機能しないため、問題に対する2つの解決策が見られます。

最初の方法
あなたが設定した IsTextSearchEnabledTrue のために ComboBox, 、オーバーライド ToString ソースクラスで変更します MultiBinding の中に TextBlockBinding

xaml

<ComboBox ...
          IsTextSearchEnabled="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>

ソースクラス

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

2番目の方法
トストリングをオーバーライドしたくない場合は、ソースクラスに組み合わせた新しいプロパティを導入する必要があると思います。 BidServiceCategoryIdBidServiceCategoryName のために TextSearch.TextPath. 。この例では、Bidservicecategoryと呼びます。これが機能するには、電話する必要があります OnPropertyChanged("BidServiceCategory"); いつ BidServiceCategoryId また BidServiceCategoryName 変化もあります。それらが通常の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>

ソースクラス

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");
        }
    }
}

他のヒント

テキスト検索ですべてのテキストを検索する必要があるかどうかはわかりませんが、カテゴリIDから検索する場合は、textsearch.textpathプロパティをBidservicecatedoryIdに設定できます。また、マルチバインディングを使用したい人にとっても役立ち、テキスト検索が機能しなくなったことがわかります... TextPathプロパティを明示的に設定すると機能します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top