简体   繁体   English

动态自定义ListBox中的TextBlock文本

[英]Customize text of TextBlock Text within ListBox dynamically

I have a ListBox with a custom DataTemplate as follows: 我有一个带有自定义DataTemplate的ListBox,如下所示:

<ListBox>
     <ListBox.ItemTemplate>
          <DataTemplate>
               <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Value}" />
               </StackPanel>
          </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

I would like to customize the TextBlock contents based on the value of another control on the page which is used to filter the items in the ListBox. 我想基于页面上另一个控件的值来自定义TextBlock内容,该控件用于过滤ListBox中的项目。 So if the filter control had the text "Hello" in it, a list item with text of "Hello World!" 因此,如果过滤器控件中包含文本“ Hello”,则列表文本为“ Hello World!”。 would appear as Hello world! 会显示为Hello world! (with "Hello" bolded). (“ Hello”加粗)。

I am not sure where to hook in this type of custom formatting. 我不确定在这种类型的自定义格式中要挂在哪里。 I thought about using a Converter, but they only support a single parameter and multivalueconverters are a no-go still in Silverlight 4. I thought about an event where I could iterate through the items, but none seems to be present in the ListBox or at the Item level. 我曾考虑过使用Converter,但它们仅支持单个参数,而在Silverlight 4中仍然无法使用multivalueconverters。我想到了一个事件,在该事件中我可以遍历所有项目,但似乎没有出现在ListBox或目录中。项目级别。 I saw this option , but I have to wonder if there is not a simpler solution to this problem. 我看到了这个选项 ,但是我想知道是否没有更简单的解决方案。

UPDATE: This is even make more complicated by the fact that I will need to use multiple RUN blocks since matches can occur in multiple locations within a string. 更新:由于需要在字符串中的多个位置进行匹配,因此我将需要使用多个RUN块,这甚至使情况变得更加复杂。 Eg Hello world Hello would have two matches. 例如,Hello world world Hello将进行两次比赛。

I think you can accomplish this with a converter. 我认为您可以使用转换器来完成此任务。 You would just pass in the value of the filter textbox in the ConverterParameter. 您只需要在ConverterParameter中传入过滤器文本框的值即可。 Your binding would look something like this: 您的绑定看起来像这样:

<TextBlock Text="{Binding Value, Converter={StaticResource YourConverterName}, ConverterParameter={ElementName=FilterTextBox, Path=Text}}" />

Convert method for reference: 转换方法以供参考:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

At that point, in your Convert method, you would have the text of the TextBlock via the value parameter, and the text of the filter TextBox via the "parameter" parameter. 那时,在您的Convert方法中,您将通过value参数获得TextBlock的文本,并通过“ parameter”参数获得过滤器TextBox的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM