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

发布于 2024-10-13 23:58:18 字数 1369 浏览 6 评论 0原文

我在 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>

I have below combo box in mvvm-wpf application. I need to implement "Text search" in this..(along with multibinding). Can anybody help me please.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-10-20 23:58:18

不幸的是,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>

但是这是行不通的,所以我看到了你的问题的两种解决方案。

第一种方式
您将 ComboBoxIsTextSearchEnabled 设置为 True,覆盖源类中的 ToString 并更改 MultiBinding TextBlock 中的Binding

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);
    }
    //...
}

第二种方式
如果您不想覆盖 ToString,我认为您必须在源类中引入一个新的属性,其中将 BidServiceCategoryIdBidServiceCategoryName 组合为 TextSearch .TextPath。在此示例中,我将其称为 BidServiceCategory。为此,您必须在 BidServiceCategoryIdBidServiceCategoryName 发生更改时调用 OnPropertyChanged("BidServiceCategory");。如果它们是普通的 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");
        }
    }
}

Unfortunately, TextSearch.Text doesn't work in a DataTemplate. Otherwise you could have done something like this

<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>

However this won't work, so I see two solutions to your problem.

First way
You set IsTextSearchEnabled to True for the ComboBox, override ToString in your source class and change the MultiBinding in the TextBlock to a Binding

Xaml

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

Source class

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

Second Way
If you don't want to override ToString I think you'll have to introduce a new Property in your source class where you combine BidServiceCategoryId and BidServiceCategoryName for the TextSearch.TextPath. In this example I call it BidServiceCategory. For this to work, you'll have to call OnPropertyChanged("BidServiceCategory"); when BidServiceCategoryId or BidServiceCategoryName changes as well. If they are normal CLR properties, you can do this in set, and if they are dependency properties you'll have to use the property changed callback

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>

Source class

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");
        }
    }
}
一袭水袖舞倾城 2024-10-20 23:58:18

我不知道您的文本搜索是否必须搜索所有文本,但如果您想从类别 ID 进行搜索,只需将 TextSearch.TextPath 属性设置为 BidServiceCategoryId 即可。这对于想要使用多重绑定并发现文本搜索不再起作用的任何人也应该有所帮助...如果您显式设置 TextPath 属性,它确实会起作用。

I don't know if your text search has to search ALL the text, but if you want to search from the category ID, you can just set the TextSearch.TextPath property to BidServiceCategoryId. That should also be helpful for anyone who wants to use multibinding and finds that the text search no longer works... It does work if you explicitly set the TextPath property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文